


When the tintColor is removed, the borders and the selected segment background color will also disappear. Next, let's remove the backgroundColor and tintColor. Don't forget to append the isActive property to each of the auto layout constraints with a value of true or they won't work. The playground live view shows us our basic UISegmentedControl. (equalToConstant: 40).isActive = true = view

(equalTo: view.widthAnchor).isActive = true // Constraining the height of the segmented control to an arbitrarily chosen value (equalTo: view.topAnchor).isActive = true // Constrain the segmented control width to be equal to the container view width Constrain the segmented control to the top of the container view anslatesAutoresizingMaskIntoConstraints = false // Add the segmented control to the container view lectedSegmentIndex = 0 // This needs to be false since we are using auto layout constraints SegmentedControl.insertSegment(withTitle: "Three", at: 2, animated: true) SegmentedControl.insertSegment(withTitle: "Two", at: 1, animated: true) SegmentedControl.insertSegment(withTitle: "One", at: 0, animated: true) Let segmentedControl = UISegmentedControl() Container view let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100)) If not, the button bar at the bottom of the selected segment might end up being too wide or not wide enough for the segment it's under. Also to note, the way I'm building out my constraints will assume all segments are of equal length. I'm doing this in a Swift playground, so let's start with the basics by creating a new UIView and adding a UISegmentedControl to it with three segments. Just a disclaimer, this is one way of doing it I'm using auto layout constraints, building the views programatically, and doing all of my theming inline for the purposes of simplicity. I found a couple of really good third-party projects that handled this, but I had some trouble with them and decided to try doing it myself. I've been working on a project and I wanted the neat "button bar-style" design for my UISegmentedControl, where there are no borders around the segments and there's a small bar below the selected segment which moves when you choose a new
