UISegmentedControl的编程约束不起作用

时间:2018-05-20 23:28:09

标签: ios uikit constraints

我正在尝试使用UIKit程序设计约束来创建一个简单的界面,其中左侧有一行UISegmentedControl,右侧有一个黄色的UIView。

问题在于我希望UIView扩展到视图的整个高度,但由于某种原因,它的底部锚点不在段控制器的实际底部。

enter image description here

这是代码

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

为什么会发生这种情况,我该如何解决?

1 个答案:

答案 0 :(得分:1)

而不是

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

制作

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