视觉约束未被识别
Visual constraints not being recognized
我只是想设置允许 UIWebView
填满屏幕的约束。 (屏幕有一个 UINavigationbar
和一个 UIToolBar
),我尝试按照苹果文档中给出的示例进行操作,但总是收到警告并且我的视图没有显示。下面是我试过的。
private func addSubviews(){
navigationController?.setToolbarHidden(false, animated: false)
// webView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
webView = UIWebView(frame: CGRectZero)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let views = ["myView" : webView]
let formatString = "|-[myView]-|"
let constraints = NSLayoutConstraint.constraintsWithVisualFormat(formatString, options:[] , metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(constraints)
}
哎呀,视觉限制让我头疼。我从来都不是视觉语言的粉丝,对我来说从来没有任何意义。这可能是更多的代码,但我认为它更容易作为开发人员阅读,并且更能表达意图。
self.view.addSubview(webView)
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Left, relatedBy: .Equal, toItem: webView, attribute: .Left, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Top, relatedBy: .Equal, toItem: webView, attribute: .Top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Bottom, relatedBy: .Equal, toItem: webView, attribute: .Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Right, relatedBy: .Equal, toItem: webView, attribute: .Right, multiplier: 1, constant: 0))
这些约束将视图的所有边固定到距离为 0 的相应边。
然后在你的约束结束时,不要忘记:
self.view.layoutIfNeeded()
我对视觉语言的问题是我不能看着它并立即清楚它的作用。在这里,它非常明确。只是为了好玩,这是一个很好的函数,您可以在常量和乘数为 0 时使用:
/**
Adds an edge constraint between the superview and subview with a constant of 0.
- parameter attribute: Layout attribute.
- parameter subview: Subview.
*/
func addEdgeConstraintWithAttribute(attribute: NSLayoutAttribute, withSubview subview: UIView) {
self.addConstraint(NSLayoutConstraint(item: subview, attribute: attribute, relatedBy: .Equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0))
}
像这样使用:
self.addEdgeConstraint(.Top, withSubview: webView)
其中函数是 UIView
的扩展
我认为,您的视图没有显示出来是因为您没有为其设置足够的约束。
基本上,对于视图,您应该添加约束以使其至少知道位置和大小。在某些情况下,您不需要设置大小限制,因为该视图具有 intrinsic content size
,例如 UILabel
、UIButton
回到你的例子,你应该像这样添加约束:
private func addSubviews(){
navigationController?.setToolbarHidden(false, animated: false)
// webView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
let webView = UIWebView(frame: CGRectZero)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let views = ["myView" : webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myView]-|", options:[] , metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myView]-|", options:[] , metrics: nil, views: views))
}
H:|-[myView]-|
中的H
代表水平。
V:|-[myView]-|
中的V
代表垂直。
我只是想设置允许 UIWebView
填满屏幕的约束。 (屏幕有一个 UINavigationbar
和一个 UIToolBar
),我尝试按照苹果文档中给出的示例进行操作,但总是收到警告并且我的视图没有显示。下面是我试过的。
private func addSubviews(){
navigationController?.setToolbarHidden(false, animated: false)
// webView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
webView = UIWebView(frame: CGRectZero)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let views = ["myView" : webView]
let formatString = "|-[myView]-|"
let constraints = NSLayoutConstraint.constraintsWithVisualFormat(formatString, options:[] , metrics: nil, views: views)
NSLayoutConstraint.activateConstraints(constraints)
}
哎呀,视觉限制让我头疼。我从来都不是视觉语言的粉丝,对我来说从来没有任何意义。这可能是更多的代码,但我认为它更容易作为开发人员阅读,并且更能表达意图。
self.view.addSubview(webView)
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Left, relatedBy: .Equal, toItem: webView, attribute: .Left, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Top, relatedBy: .Equal, toItem: webView, attribute: .Top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Bottom, relatedBy: .Equal, toItem: webView, attribute: .Bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: .Right, relatedBy: .Equal, toItem: webView, attribute: .Right, multiplier: 1, constant: 0))
这些约束将视图的所有边固定到距离为 0 的相应边。
然后在你的约束结束时,不要忘记:
self.view.layoutIfNeeded()
我对视觉语言的问题是我不能看着它并立即清楚它的作用。在这里,它非常明确。只是为了好玩,这是一个很好的函数,您可以在常量和乘数为 0 时使用:
/**
Adds an edge constraint between the superview and subview with a constant of 0.
- parameter attribute: Layout attribute.
- parameter subview: Subview.
*/
func addEdgeConstraintWithAttribute(attribute: NSLayoutAttribute, withSubview subview: UIView) {
self.addConstraint(NSLayoutConstraint(item: subview, attribute: attribute, relatedBy: .Equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0))
}
像这样使用:
self.addEdgeConstraint(.Top, withSubview: webView)
其中函数是 UIView
我认为,您的视图没有显示出来是因为您没有为其设置足够的约束。
基本上,对于视图,您应该添加约束以使其至少知道位置和大小。在某些情况下,您不需要设置大小限制,因为该视图具有 intrinsic content size
,例如 UILabel
、UIButton
回到你的例子,你应该像这样添加约束:
private func addSubviews(){
navigationController?.setToolbarHidden(false, animated: false)
// webView = UIWebView(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height))
let webView = UIWebView(frame: CGRectZero)
webView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(webView)
let views = ["myView" : webView]
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myView]-|", options:[] , metrics: nil, views: views))
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[myView]-|", options:[] , metrics: nil, views: views))
}
H:|-[myView]-|
中的H
代表水平。
V:|-[myView]-|
中的V
代表垂直。