首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >视觉约束不被识别

视觉约束不被识别
EN

Stack Overflow用户
提问于 2016-04-21 05:01:25
回答 2查看 63关注 0票数 0

我只想设置允许UIWebView填充屏幕的约束。(屏幕上有一个UINavigationbar和一个UIToolBar),我试着遵循苹果文档中的例子,但是总是收到警告,我的视图没有显示出来。下面是我尝试过的。

代码语言:javascript
运行
复制
 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)
} 
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-21 05:18:39

哦,视觉上的限制让我的头疼。我从来不是视觉语言的粉丝,对我来说也没有任何意义。这可能是更多的代码,但我认为作为一个开发人员更容易阅读,而意图则更有表现力。

代码语言:javascript
运行
复制
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的相应边缘上。

然后,在你的约束结束时,不要忘记:

代码语言:javascript
运行
复制
self.view.layoutIfNeeded()

我对视觉语言的问题是,我不能看它,并立即清楚它所做的事情。在这里,这是非常明确的。为了好玩,这里有一个很好的函数,只要常数和乘数为0,就可以使用:

代码语言:javascript
运行
复制
/**
 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))
}

用于:

代码语言:javascript
运行
复制
self.addEdgeConstraint(.Top, withSubview: webView)

其中函数是UIView上的一个扩展

票数 0
EN

Stack Overflow用户

发布于 2016-04-21 06:03:57

我认为,你的观点没有出现,因为你没有为它设置足够的限制。

基本上,在视图中,您应该添加约束,让它至少知道位置和大小。在某些情况下,您不需要设置大小约束,因为该视图具有intrinsic content size,如UILabelUIButton

回到您的示例,您应该添加如下约束:

代码语言:javascript
运行
复制
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]-|表示垂直。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36760052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档