Search code examples
swiftxcodeuitableviewuisegmentedcontrol

Add constants to segmented controls in TableView


I added a segmented Control to the tableView.

It spanned the entire width of the screen, how to add constraints to it on the left and right?

In storyboard constraints are not active, they cannot be configured.enter image description here


Solution

  • From the looks of it you added it to the TableHeaderView of your UITableViewController. Add a simple UIView as a container into the TableHeaderView first. You can move your UISegmentedControl into the container at which point you will be able to define constraints (such as the leading, trailing-, top- and bottom-anchors or the height-anchor of the UISegmentedControl).

    For the container itself you have to define a height, make sure it can fit the contents based on their constraints. From my experience TableHeaderViews don't play so well with programmatically changing their heights at runtime. This however should not matter for your current scenario.