0
votes

I am trying to use UIKit programatic constraints to create a simple interface where there is a row with a UISegmentedControl on the left and a yellow UIView on the right.

The problem is that I would like that UIView to extend for the full height of the view however for some reason its bottom anchor is not at the actual bottom of the segment controller.

enter image description here

Here is the code

parentGuide = top.layoutMarginsGuide

let choices = UISegmentedControl(items: ["Op1", "Op2"])
choices.translatesAutoresizingMaskIntoConstraints = false
top.addSubview(choices)
choices.leadingAnchor.constraint(equalTo: parentGuide.leadingAnchor).isActive = true
choices.topAnchor.constraint(equalTo: parentGuide.topAnchor).isActive = true

let yellow = UIView()
yellow.translatesAutoresizingMaskIntoConstraints = false
top.addSubview(yellow)
yellow.backgroundColor = UIColor.yellow
yellow.trailingAnchor.constraint(equalTo: parentGuide.trailingAnchor).isActive = true
yellow.topAnchor.constraint(equalTo: parentGuide.topAnchor).isActive = true
yellow.leadingAnchor.constraint(equalTo: parentGuide.trailingAnchor, constant: -40).isActive = true
yellow.bottomAnchor.constraint(equalTo: choices.layoutMarginsGuide.bottomAnchor).isActive = true

Why is this happening and how can I fix it?

1

1 Answers

1
votes

Instead of

yellow.bottomAnchor.constraint(equalTo: choices.layoutMarginsGuide.bottomAnchor).isActive = true

make it

yellow.bottomAnchor.constraint(equalTo: choices.bottomAnchor).isActive = true