gpt4 book ai didi

ios - 如何以编程方式对按钮施加约束(水平间距)

转载 作者:行者123 更新时间:2023-11-29 02:02:48 24 4
gpt4 key购买 nike

我想并排放置两个按钮。

一个 ( testButton ) 当前位于正确的位置 - 垂直和水平对齐 - 使用 Interface Builder 自动布局。

让我们看看:

Interface builder - auto layout - currently

我想以编程方式并排放置另一个 (okButton):

我想要的预览:

Interface builder - auto layout - result

enter image description here

我添加了一个 CenterY 约束和一个 Leading/Trailing 约束,但它不起作用,测试按钮的 UIButton 宽度变大了..

让我们看一下Debug View Hierarchy:

Interface builder - auto layout - Debug View Hierarchy

还有我的代码:

override func viewDidLoad() {
super.viewDidLoad()

let constraintAlignY = NSLayoutConstraint(
item: okButton,
attribute: .CenterY,
relatedBy: .Equal,
toItem: testButton,
attribute: .CenterY,
multiplier: 1,
constant: 0)

let constraintXSpace = NSLayoutConstraint(
item: okButton,
attribute: .Leading,
relatedBy: .Equal,
toItem: testButton,
attribute: .Trailing,
multiplier: 1,
constant: 8)

view.addConstraint(constraintAlignY)
view.addConstraint(constraintXSpace)
}

警告控制台:

2015-05-09 23:03:10.439 MainThreading[10227:663061] 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:0x7b8c8ee0 UIButton:0x7b8cde90'test'.centerY == UIView:0x7b8ced40.centerY>",
"<NSIBPrototypingLayoutConstraint:0x7b8c40f0 'IB auto generated at build time for view with fixed frame' V:|-(191)-[UIButton:0x7b8ce8d0'ok'] (Names: '|':UIView:0x7b8ced40 )>",
"<NSIBPrototypingLayoutConstraint:0x7b8c77f0 'IB auto generated at build time for view with fixed frame' V:[UIButton:0x7b8ce8d0'ok'(30)]>",
"<NSLayoutConstraint:0x7b8c4f50 UIButton:0x7b8ce8d0'ok'.centerY == UIButton:0x7b8cde90'test'.centerY>",
"<NSLayoutConstraint:0x7b8da380 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7b8ced40(480)]>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7b8c4f50 UIButton:0x7b8ce8d0'ok'.centerY == UIButton:0x7b8cde90'test'.centerY>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
(lldb)

有什么想法吗?谢谢!

最佳答案

如果您添加一个 View ,并且自己不添加任何约束,系统会为您添加它们(到顶部和左侧),所以我认为这就是问题所在。

系统添加的约束与您在代码中添加的约束相冲突。由于您的最终目标是动态添加 View ,因此您应该在代码中添加“确定”按钮而不是 IB。对于任何代码添加的 View ,请务必将 translatesAutoresizingMaskIntoConstraints 设置为 false

如果您确实需要在 IB 中添加该按钮,则为其提供约束,但通过选中“在构建时删除”复选框来删除它们。然后在代码中添加新的是安全的。

关于ios - 如何以编程方式对按钮施加约束(水平间距),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30145043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com