[英]How can I display views using autolayout constraints in Xcode playground?
I am trying to display views configured with autolayout constraints in XCode playground , but it doesn't seem to work. 我试图显示与XCode的自动布局约束操场配置的看法,但它似乎没有工作时。 It's like playground ignores the constraints completely, and I can't find information about this issue anywhere. 就像游乐场完全忽略了约束,而我在任何地方都找不到有关此问题的信息。
Here's the code I tried: 这是我尝试的代码:
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)
Thanks in advance 提前致谢
That being said you can use the liveView
property of PlaygroundPage
to preview your UI. 话虽如此,您可以使用PlaygroundPage
的liveView
属性预览您的UI。
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
Here's a link that expands on this further. 这是进一步扩展此链接的链接 。
If its not working you may need to trigger a layoutIfNeeded
but that shouldn't really solve the issue since Swift 4 如果它不起作用,则可能需要触发layoutIfNeeded
但是自Swift 4起,那应该不能真正解决问题
Also, include translatesAutoresizingMaskIntoConstraints = false
on your view. 另外,在您的视图上包括translatesAutoresizingMaskIntoConstraints = false
。 Like: 喜欢:
import PlaygroundSupport
myLiveView.translatesAutoresizingMaskIntoConstraints = false
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
It seems that now you need to do something like this: 看来现在您需要执行以下操作:
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
My final code; 我的最终代码;
If you only see the yellow outer UIView then definitely need: 如果您只看到黄色的外部UIView,则绝对需要:
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
And to make sure view is updating change the text of the label 并确保视图正在更新,更改标签的文本
label.text = "I am a label3 "
FULL CODE 完整代码
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
The provided solutions were not useful for me. 提供的解决方案对我没有用。 I wanted a view in which I could work with constraints. 我想要一个可以处理约束的视图。 To achieve that I had to create a containerView with a CGRect base and adding constraints to it as well. 为此,我必须创建一个具有CGRect基础的containerView并向其添加约束。 Everything else was not successful. 其他一切都不成功。 So I got a viewable base and was able to add views just with constraints. 因此,我有了一个可见的基础,并且能够添加带有约束的视图。
Setup 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
Place View in ContainerView with constraints: 将视图放置在具有限制的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
This creates this view: 这将创建以下视图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.