NSLayoutConstraint 不适用于 uibutton 和 uilabel
NSLayoutConstraint not working on uibutton and uilabel
我创建了一个 uilable 和 uibutton
@IBOutlet weak var label_one: UILabel!
@IBOutlet weak var btn_one: UIButton!
并设置以下尾随和前导约束
self.btn_one.translatesAutoresizingMaskIntoConstraints=false
let contariants=NSLayoutConstraint(item:self.view,attribute:NSLayoutAttribute.LeadingMargin , relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 20)
let trailingConstraints=NSLayoutConstraint(item: self.view, attribute:NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute:NSLayoutAttribute.Trailing , multiplier: 1.0, constant:20 )
self.view.addConstraint(contariants)
self.view.addConstraint(trailingConstraints)
// 但它根本不起作用并发出以下警告
可能至少以下列表中的约束之一是您不想要的。
试试这个:
(1)查看每个约束并尝试找出您不期望的;
(2) 找到添加了不需要的约束或约束的代码并修复它。
(
"",
"UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>"
)
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。
中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
2015-12-16 10:39:01.918 ConstraintsExample[2635:38168] 无法同时满足约束。
可能至少以下列表中的约束之一是您不想要的。
试试这个:
(1)查看每个约束并尝试找出您不期望的;
(2) 找到添加了不需要的约束或约束的代码并修复它。
(
"",
"UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>",
"",
“”
)
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。
中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
此错误是因为您试图让视图同时执行多个约束。在您从 IB 将元素拖放到视图上的那一刻,它就对给定位置进行了基本约束。
当您尝试以下 "addConstraint" 方法时,编译器无法理解要遵循哪个约束。
如果您以编程方式制作按钮和标签,以上内容将有效。 PFB一个post 说说这个:
Programmatically Add CenterX/CenterY Constraints
如果您想修改 Storyboard 上的现有约束:
在元素上添加约束:
不要将该约束用作与您的代码的连接:
您按照错误消息中的说明进行操作。您查看它显示的约束条件,仔细思考,并找出您的约束条件相互矛盾的地方。
您可能在 .xib 文件或情节提要中有一些限制。
顺便说一句。 "contariants" 太可怕了。第一个约束看起来不对(view.leading = button.leading + 20,这意味着按钮从屏幕左侧 20 像素处开始????)。 NSLayoutAttribute.LeadingMargin 应该只是 .LeadingMargin。
我创建了一个 uilable 和 uibutton
@IBOutlet weak var label_one: UILabel!
@IBOutlet weak var btn_one: UIButton!
并设置以下尾随和前导约束
self.btn_one.translatesAutoresizingMaskIntoConstraints=false
let contariants=NSLayoutConstraint(item:self.view,attribute:NSLayoutAttribute.LeadingMargin , relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 20)
let trailingConstraints=NSLayoutConstraint(item: self.view, attribute:NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: btn_one, attribute:NSLayoutAttribute.Trailing , multiplier: 1.0, constant:20 )
self.view.addConstraint(contariants)
self.view.addConstraint(trailingConstraints)
// 但它根本不起作用并发出以下警告 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 ( "", "UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>" )
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。 2015-12-16 10:39:01.918 ConstraintsExample[2635:38168] 无法同时满足约束。 可能至少以下列表中的约束之一是您不想要的。 试试这个: (1)查看每个约束并尝试找出您不期望的; (2) 找到添加了不需要的约束或约束的代码并修复它。 ( "", "UIButton:0x7ff8c84b0b60'Button' (Names: '|':UIView:0x7ff8c85226d0 )>", "", “” )
将尝试通过打破约束来恢复
在 UIViewAlertForUnsatisfiableConstraints 处创建符号断点以在调试器中捕获此问题。 中列出的 UIView 的 UIConstraintBasedLayoutDebugging 类别中的方法也可能有帮助。
此错误是因为您试图让视图同时执行多个约束。在您从 IB 将元素拖放到视图上的那一刻,它就对给定位置进行了基本约束。 当您尝试以下 "addConstraint" 方法时,编译器无法理解要遵循哪个约束。
如果您以编程方式制作按钮和标签,以上内容将有效。 PFB一个post 说说这个:
Programmatically Add CenterX/CenterY Constraints
如果您想修改 Storyboard 上的现有约束:
在元素上添加约束:
不要将该约束用作与您的代码的连接:
您按照错误消息中的说明进行操作。您查看它显示的约束条件,仔细思考,并找出您的约束条件相互矛盾的地方。
您可能在 .xib 文件或情节提要中有一些限制。
顺便说一句。 "contariants" 太可怕了。第一个约束看起来不对(view.leading = button.leading + 20,这意味着按钮从屏幕左侧 20 像素处开始????)。 NSLayoutAttribute.LeadingMargin 应该只是 .LeadingMargin。