我有一个我想看起来像这样的视图:
|---------------|
| | <- navBar
|---------------|
| | <- topView
|---------------|
| |
| |
| |
|---------------|
我想要的只是将 topView.top 粘贴到 navBar.bottom 上。我决定使用制图并实现以下代码(试图坚持使用 MVC ofc):
在我的UIViewController
子类中:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
aView?.topLayoutGuide = self.topLayoutGuide // where aView is my subclass of UIView, inserted in loadView method
}
在我的UIView
子类中:
var topLayoutGuide: UILayoutSupport?
override func updateConstraints() {
var constraint = NSLayoutConstraint?()
layout(topView) { (topView) in
constraint = topView.top == topView.superview!.top
}
topView.superview!.removeConstraint(constraint!)
layout(topView) { topView in
topView.top == topView.superview!.top + (self.topLayoutGuide?.length ?? 0)
topView.height == 67
topView.left == topView.superview!.left
topView.width == topView.superview!.width
}
super.updateConstraints()
}
问题是我收到以下日志,但我不知道如何修复它:
Unable to simultaneously satisfy constraints.
[...]
(
"<NSLayoutConstraint:0x7fe6a64e4800 V:|-(64)-[UIView:0x7fe6a6505d80] (Names: '|':MyApp.MyView:0x7fe6a360d4c0 )>",
"<NSLayoutConstraint:0x7fe6a3538a80 V:|-(0)-[UIView:0x7fe6a6505d80] (Names: '|':MyApp.MyView:0x7fe6a360d4c0 )>"
)
看来我需要一些帮助。如何正确地做到这一点?我不想在其中实施约束,UIViewController
也不想使用Storyboards
.
谢谢你的帮助!