问题
I am trying to display views configured with autolayout constraints in XCode playground, but it doesn't seem to work. 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
回答1:
That being said you can use the liveView
property of PlaygroundPage
to preview your 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
Also, include translatesAutoresizingMaskIntoConstraints = false
on your view. Like:
import PlaygroundSupport
myLiveView.translatesAutoresizingMaskIntoConstraints = false
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = myLiveView
回答2:
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
回答3:
My final code;
If you only see the yellow outer UIView then definitely need:
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
回答4:
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. Everything else was not successful. So I got a viewable base and was able to add views just with constraints.
Setup 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:
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:
来源:https://stackoverflow.com/questions/30333323/how-can-i-display-views-using-autolayout-constraints-in-xcode-playground