gpt4 book ai didi

ios - 视觉限制未被识别

转载 作者:行者123 更新时间:2023-11-30 13:29:16 25 4
gpt4 key购买 nike

我只是想设置允许 UIWebView 填充屏幕的约束。 (屏幕有一个 UINavigationbar 和一个 UIToolBar),我尝试按照苹果文档中给出的示例进行操作,但总是收到警告,并且我的 View 没有显示。以下是我尝试过的。

 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))

这些约束将 View 的所有边缘固定到距离为 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 上的扩展

关于ios - 视觉限制未被识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36760052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com