我想,以显示与配置的观点自动布局XCode中限制游乐场,但它似乎没有工作时。就像游乐场完全忽略了约束,而我在任何地方都找不到有关此问题的信息。
这是我尝试的代码:
let view = UIView()
view.frame = CGRectMake(0, 0, 400, 200)
view.backgroundColor = UIColor.lightGrayColor()
let label = UILabel() // I can only see the label if I set a frame
// UILabel(frame: CGRectMake(0, 0, 200, 50))
label.backgroundColor = UIColor.greenColor()
label.text = "I am a label"
label.setTranslatesAutoresizingMaskIntoConstraints(false)
view.addSubview(label)
let views = ["label":label]
let options = NSLayoutFormatOptions(0)
let cs1 = NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-[label]-|", options: options, metrics: nil, views:views )
let cs2 = NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-[label]-|", options: options, metrics: nil, views:views )
view.addConstraints(cs1)
view.addConstraints(cs2)
提前致谢
话虽这么说,您可以使用liveView
属性PlaygroundPage
来预览UI。
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
这是进一步扩展此链接的链接。
如果无法正常工作,您可能需要触发一个,layoutIfNeeded
但是自Swift 4起,那应该不能真正解决问题
另外,请包括translatesAutoresizingMaskIntoConstraints = false
在您的视图中。喜欢:
import PlaygroundSupport
myLiveView.translatesAutoresizingMaskIntoConstraints = false
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句