【发布时间】:2014-08-20 20:36:23
【问题描述】:
由于与 AutoLayout 相关的问题非常多,我不完全确定我在问一个新问题,但我就是无法让它发挥作用。我正在尝试使用 AutoLayout 为视图设置动画,但我在翻译它时已经失败了。在使用情节提要之前,我曾与 AL 合作过,这是我第一次尝试以编程方式使用它们。
所以,对于问题:从一个新创建的主从 iOS 应用程序开始,我从情节提要中删除了所有内容,插入了一个新的视图控制器并将其视图的类设置为 BaseView。基本视图代码如下:
class BaseView: UIView {
@IBOutlet var secondaryView: UIView! = nil
override func awakeFromNib() {
setTranslatesAutoresizingMaskIntoConstraints(false)
backgroundColor = UIColor.redColor()
secondaryView = UIView(frame: frame)
secondaryView.backgroundColor = UIColor.blueColor()
addSubview(secondaryView)
let views = ["secondaryView": secondaryView, "self": self]
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-(100)-[secondaryView]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: views))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[secondaryView(==self)]", options: NSLayoutFormatOptions(0), metrics: nil, views: views))
}
}
我还尝试通过情节提要的用户定义运行时属性将 translatesAutoresizingMaskIntoConstraints 设置为 false,但无济于事。我期望在模拟器的左侧看到一个红色方块(基本视图),而模拟器屏幕的其余部分则填充为蓝色。相反,一切都是蓝色的,日志给了我以下错误:
2014-08-20 22:19:00.392 AutoLayoutAnimationTest[5585:473139] 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:0x7fa8c94b5f50 H:|-(100)-[UIView:0x7fa8c94b2540] (Names: '|':AutoLayoutAnimationTest.BaseView:0x7fa8c94afa80 )>",
"<NSAutoresizingMaskLayoutConstraint:0x7fa8c94bdfc0 h=--& v=--& UIView:0x7fa8c94b2540.midX == + 300>",
"<NSAutoresizingMaskLayoutConstraint:0x7fa8c94be1d0 h=--& v=--& H:[UIView:0x7fa8c94b2540(600)]>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7fa8c94b5f50 H:|-(100)-[UIView:0x7fa8c94b2540] (Names: '|':AutoLayoutAnimationTest.BaseView:0x7fa8c94afa80 )>
我不知道这些NSAutoresizingMaskLayoutConstraints 来自哪里,但据我了解,它们正在阻止secondaryView 移动。我做错了什么?
【问题讨论】:
标签: ios swift autoresizingmask autolayout