我只想设置允许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)
} 发布于 2016-04-21 05:18:39
哦,视觉上的限制让我的头疼。我从来不是视觉语言的粉丝,对我来说也没有任何意义。这可能是更多的代码,但我认为作为一个开发人员更容易阅读,而意图则更有表现力。
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上的一个扩展
发布于 2016-04-21 06:03:57
我认为,你的观点没有出现,因为你没有为它设置足够的限制。
基本上,在视图中,您应该添加约束,让它至少知道位置和大小。在某些情况下,您不需要设置大小约束,因为该视图具有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 in H:|-[myView]-|表示水平。
V in V:|-[myView]-|表示垂直。
https://stackoverflow.com/questions/36760052
复制相似问题