如何在Xcode游乐场中使用自动布局约束来显示视图?
我试图显示在XCode 操场configuration自动布局约束的意见,但似乎并没有工作。 这就像游乐场完全忽略了这个限制,而且我无法在任何地方find关于这个问题的信息。
这是我尝试的代码:
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)
提前致谢
这就是说你可以使用PlaygroundPage
的liveView
属性来预览你的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
看来,现在你需要做这样的事情:
import UIKit import XCPlayground let main = UIView(frame: CGRect(x: 0, y: 0, width: 320, height: 700)) main.backgroundColor = .blackColor() XCPlaygroundPage.currentPage.liveView = main // now add views and constraints to main main // display view
我的最终代码
如果你只看到黄色的外部UIView,那么肯定需要:
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
并确保视图正在更新更改标签的文本
label.text = "I am a label3 "
完整的代码
import UIKit import XCPlayground //http://stackoverflow.com/questions/30333323/how-can-i-display-views-using-autolayout-constraints-in-xcode-playground/30336502#30336502 let view = UIView() view.frame = CGRectMake(0, 0, 300, 200) view.backgroundColor = UIColor.yellowColor() //------------------------------------------------------------- XCPlaygroundPage.currentPage.liveView = view //needed else label may not appear XCPlaygroundPage.currentPage.needsIndefiniteExecution = true //------------------------------------------------------------- //LABEL //------------------------------------------------------------- let label = UILabel(frame: CGRectMake(0, 0,200, 50)) label.backgroundColor = UIColor.greenColor() label.textColor = UIColor.redColor() //TO FORCE REFRESH change text of label label.text = "I am a label3 " //add constraints label.translatesAutoresizingMaskIntoConstraints = false //COULDNT GET TO WORK view.addSubview(label) //-------------------------------------------------------------- //COMMENT OUT TO SEE LABEL with out constraints //-------------------------------------------------------------- let views = ["label":label] let options = NSLayoutFormatOptions(arrayLiteral:[]) 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) //XCPlaygroundPage.currentPage.liveView = view