我有一种看法,我想这样看:
|---------------|
| | <- 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。
谢谢你的帮助!
发布于 2015-07-22 17:42:31
一种解决方案:创建对特定NSLayoutConstraint的引用,并更新其constant
var topLayoutConstraint: NSLayoutConstraint!
override func updateConstraints() {
layout(topView) { topView in
topLayoutConstraint = topView.top == topView.superview!.top
topView.height == 67
topView.left == topView.superview!.left
topView.width == topView.superview!.width
}
super.updateConstraints()
}..。然后,您可以像这样调整NSLayoutConstraint的常量:
topLayoutConstraint.constant = 50剩下的唯一问题是-什么时候更新topLayoutConstraint?一个解决方案是创建一个协议,MyViewInterface
protocol MyViewInterface: class {
var topLayoutGuideLength: CGFloat { get set }
}然后,在UIView中,当更改topLayoutGuideLength属性时,调整NSLayoutConstraint的constant
public class MyView: UIView, MyViewInterface {
public var topLayoutGuideLength: CGFloat = 0 {
didSet {
topLayoutConstraint.constant = topLayoutGuideLength
}
}
}最后,在我们的UIViewController ( topLayoutGuide所在的地方)中:
public class myViewController: UIViewController {
private var myView: MyView
// ...
public override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
myView.topLayoutGuideLength = topLayoutGuide.length
}
}最终结果是什么?每当在我们的UIViewController上触发UIViewController时,我们的myView的topLayoutGuideLength属性就会被更新,这会触发对topLayoutConstraint的constant的更改。
发布于 2016-11-17 00:24:17
地图学的另一个缺乏文件..。
我在地图学中遇到了topLayoutGuideCartography和bottomLayoutGuideCartography属性作为UIViewController扩展。使用它们可以在不覆盖viewDidLayoutSubviews的情况下给出所需的输出。
您可以简单地使用此属性锚定视图的顶部。
override func updateConstraints() {
layout(topView) { topView in
topView.top == topLayoutGuideCartography
topView.height == 67
topView.left == topView.superview!.left
topView.width == topView.superview!.width
}
super.updateConstraints()
}https://stackoverflow.com/questions/30997463
复制相似问题