Jevon Cowell
Jevon Cowell

Reputation: 411

Custom Segmented Control width code not working intentionally

Here is a code for a Custom Segmented Control where I can set the appearance:

/*
     Init SMsegmentView
     Set divider colour and width here if there is a need
     */
    let segmentFrame = CGRect(x: self.margin, y: 64.0, width: self.view.frame.size.width - self.margin*1, height: 50.0)
    self.segmentView = SMSegmentView(frame: segmentFrame, dividerColour: UIColor(white: 0.95, alpha: 0.3), dividerWidth: 1.0, segmentAppearance: appearance)
    self.segmentView.backgroundColor = UIColor.clear

    self.segmentView.layer.cornerRadius = 0.0
    self.segmentView.layer.borderColor = UIColor(white: 0.85, alpha: 1.0).cgColor
    self.segmentView.layer.borderWidth = 1.0

    // Add segments
    self.segmentView.addSegmentWithTitle("Name", onSelectionImage: UIImage(named: "clip_light"), offSelectionImage: UIImage(named: "clip"))
    self.segmentView.addSegmentWithTitle("Price", onSelectionImage: UIImage(named: "bulb_light"), offSelectionImage: UIImage(named: "bulb"))
    self.segmentView.addSegmentWithTitle("Subject", onSelectionImage: UIImage(named: "cloud_light"), offSelectionImage: UIImage(named: "cloud"))

    self.segmentView.addTarget(self, action: #selector(selectSegmentInSegmentView(segmentView:)), for: .valueChanged)

    // Set segment with index 0 as selected by default
    self.segmentView.selectedSegmentIndex = 0
o   self.view.addSubview(self.segmentView)

The Only problem is that only the right side of the Segmented Control touches the left side of the screen, the right doesn't. Any idea why this is? I can provide more code if needed.

Here is a view of what the control does: SegmentedControl

Upvotes: 0

Views: 170

Answers (1)

zombie
zombie

Reputation: 5269

you need two margins one on the right and one on the left so the width should be

let width = self.view.frame.size.width - self.margin*2
let segmentFrame = CGRect(x: self.margin, y: 64.0, width: width, height: 50.0)

Upvotes: 1

Related Questions