使用 NSLayoutConstraint 在主视图中定位两个 UIButton 元素
Posted
技术标签:
【中文标题】使用 NSLayoutConstraint 在主视图中定位两个 UIButton 元素【英文标题】:Positioning two UIButton elements in the main view using NSLayoutConstraint 【发布时间】:2014-07-27 08:44:36 【问题描述】:我确定我在这里遗漏了一些非常简单的东西。我正在阅读有关使用 Auto Layout 以编程方式工作的文档,并且我正在尝试按照说明将两个按钮放在一起。
这是我的代码:
override func viewDidLoad()
super.viewDidLoad()
view.backgroundColor = UIColor.grayColor()
var button1 = UIButton()
button1.titleLabel.text = "foo"
button1.backgroundColor = UIColor.redColor()
var button2 = UIButton()
button2.titleLabel.text = "bar"
button2.backgroundColor = UIColor.purpleColor()
view.addSubview(button1)
view.addSubview(button2)
var bindings = [ "button1" : button1, "button2" : button2 ]
// picked this arbitrarily ...
var opt = NSLayoutFormatOptions.AlignAllTop
var formatString = "[button1]-12-[button2]"
var c = NSLayoutConstraint.constraintsWithVisualFormat(formatString, options: opt, metrics: nil, views: bindings)
view.addConstraints(c)
当我运行它时,我得到了错误:
2014-07-27 01:41:43.597 dynamiclayout[2366:53275] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand,
refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSLayoutConstraint:0x7be6fda0 H:[UIButton:0x7be69a00]-(12)-[UIButton:0x7be6d200]>",
"<NSAutoresizingMaskLayoutConstraint:0x7be74c20 h=--& v=--& UIButton:0x7be69a00.midX ==>",
"<NSAutoresizingMaskLayoutConstraint:0x7be74d40 h=--& v=--& H:[UIButton:0x7be69a00(0)]>",
"<NSAutoresizingMaskLayoutConstraint:0x7be723f0 h=--& v=--& UIButton:0x7be6d200.midX ==>"
)
如果我知道更多信息,我相信这条错误消息会很有帮助,但我不确定从哪里开始解析它。我在这里做错了什么?
【问题讨论】:
【参考方案1】:如果您以编程方式添加约束,则必须禁用 TranslatesAutoresizingMaskIntoConstraints
到 false
使用下面的代码,它将绘制按钮,而不是设置titleLable.text
,而是使用button1.setTitle("foo", forState: UIControlState.Normal)
,因为这是将标题设置为按钮的方法
override func viewDidLoad()
view.backgroundColor = UIColor.grayColor()
var button1 = UIButton()
button1.setTranslatesAutoresizingMaskIntoConstraints(false)
button1.setTitle("foo", forState: UIControlState.Normal)
button1.backgroundColor = UIColor.redColor()
var button2 = UIButton()
button2.setTitle("bar", forState: UIControlState.Normal)
button2.backgroundColor = UIColor.purpleColor()
button2.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(button1)
self.view.addSubview(button2)
var bindings = [ "button1" : button1, "button2" : button2 ]
// picked this arbitrarily ...
var opt = NSLayoutFormatOptions.AlignAllTop
var formatString = "[button1]-12-[button2]"
var c = NSLayoutConstraint.constraintsWithVisualFormat(formatString, options: opt, metrics: nil, views: bindings)
view.addConstraints(c)
【讨论】:
这很棒!感谢您抽出宝贵时间撰写建设性且有用的答案。以上是关于使用 NSLayoutConstraint 在主视图中定位两个 UIButton 元素的主要内容,如果未能解决你的问题,请参考以下文章
如何在 SnapKit 中设置 heightAnchor 的乘数 - swift