- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个 View ,我希望它看起来像这样:
|---------------|
| | <- 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
。
感谢您的帮助!
最佳答案
一个解决方案:创建对特定 NSLayoutConstraint
的引用,并更新其常量
:
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
?一种解决方案是创建一个协议(protocol),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
上触发 viewDidLayoutSubviews()
时,我们的 myView
的 topLayoutGuideLength
属性就会更新,这会触发更改 topLayoutConstraint
的 constant
。
关于ios - 使用 TopLayoutGuide 的制图约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30997463/
我在使用 Swift 2.0 编写的应用程序上使用 AutoLayout 和代码,并使用 Cartograph 来帮助解决约束。但有些东西阻止了我的应用程序,否则,我用它以编程方式添加 View 。
我必须创建一张 map ,为此我安装了 google cartographer 并使用了 3D 相机,通过它可以获取周围环境的数据。我正在阅读制图师的文档,但我找不到向我解释如何绘制 map 的地方。
我是 Web GIS 制图的新手,我有兴趣了解 WEB GIS 以及需要哪些技能。 我知道 Arcgis Desktop、FME、VBA、Microstation 和 Autocad。 请指导我。 最
我正在处理一个项目,该项目使用 C/C++ 语言在 Visual Studio 2010 中使用箱线图处理数据和显示信息。 我在 MSDN Microsoft documentation 上找到了一些
我是一名优秀的程序员,十分优秀!