RILEY
RILEY

Reputation: 65

Highlighting a active Segmented Control + adding a border around the active segment

I am trying to create a boarder around only the selected SegmentedControl item. The code that I have is creating the border that I am looking for, however it is creating this for both buttons in the segmented Control:

Here is the code:

sender.layer.borderColor = UIColor(white: 1.0, alpha: 1.0).CGColor
sender.layer.borderWidth = 4

I am trying to only show a border around the selected SegmentedControl.

I believe that I will need to use either setSelected or isSelected to try and only have a border around the active controller. Just need a little more guidance.

Upvotes: 0

Views: 978

Answers (2)

zisoft
zisoft

Reputation: 23078

UISegmentedControl has subViews for each of its buttons, which you can access by its subViews array:

(mySegmentedControl.subViews[mySegmentedControl.selectedSegmentIndex] as! UIView).layer.borderWidth = 4

Of course, you have to set it back to the normal borderWidth when the selection changes.

Upvotes: 1

PowHu
PowHu

Reputation: 2189

What you need to do is customize segmentedControl.

Set a border image for segmentedControl for "UIControlStateSelected"

setBackgroundImage:forState:barMetrics:

Upvotes: 0

Related Questions