我正在尝试在XCode playground 中显示使用自动布局约束配置的视图,但它似乎不起作用。这就像游乐场完全忽略了约束,我无法在任何地方找到有关此问题的信息。
这是我尝试过的代码:
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)
提前致谢
答案 0 :(得分:16)
话虽如此,您可以使用liveView
的{{1}}属性来预览您的用户界面。
PlaygroundPage
这里的link进一步扩展了这一点。
如果它无法正常工作,您可能需要触发import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
,但自Swift 4以来不应该真正解决问题
另外,在您的观看中加入layoutIfNeeded
。像:
translatesAutoresizingMaskIntoConstraints = false
答案 1 :(得分:0)
现在看来你需要做这样的事情:
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
答案 2 :(得分:0)
我的最终代码;
如果你只看到外面的黄色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
答案 3 :(得分:0)
提供的解决方案对我没有用。我想要一个可以处理约束的视图。为此,我必须创建一个具有CGRect基础的containerView并向其添加约束。其他一切都不成功。所以我有了一个可见的基础,并且能够添加带有约束的视图。
设置ContainerView:
let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 600, height: 400))
containerView.backgroundColor = UIColor.lightGray
containerView.translatesAutoresizingMaskIntoConstraints = false
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = containerView
containerView.widthAnchor.constraint(equalToConstant: 600).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 400).isActive = true
将视图放置在具有约束的ContainerView中
let myView = UIView(frame: .zero)
myView.backgroundColor = .green
myView.translatesAutoresizingMaskIntoConstraints = false
containerView.addSubview(myView)
myView.widthAnchor.constraint(equalToConstant: 200).isActive = true
myView.heightAnchor.constraint(equalToConstant: 200).isActive = true
myView.centerXAnchor.constraint(equalTo: containerView.centerXAnchor).isActive = true
myView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
这将创建此视图: