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
To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.
Control-Dragging ConstraintsIf you drag more or less horizontally, you get options to set the horizontal spacing between the views, and options to vertically align the views. If you drag more or less vertically, you get options to set the vertical spacing, and options to align the views horizontally.
Auto Layout is a constraint-based layout system designed for building dynamically sized user interfaces. It lets you create user interface layouts that dynamically adapt for all screen sizes without manually setting the frame of every view.
Auto Layout constraints allow us to create views that dynamically adjust to different size classes and positions. The constraints will make sure that your views adjust to any size changes without having to manually update frames or positions.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With