How to set the width of playground liveview?

123 views Asked by At

As shown in below, I just code to see a square ImageView, in LiveView, it will only show partially. I cannot scroll the view left or right, but can only need to drag the view left (and shrink my coding editor view) in order to see the full cyan color ImageView.

enter image description here

My question is

  1. How to get the liveview shown by default aligned left (e.g. showing the entire left part of the view OR
  2. Is there a way to define the width of the liveview, so that I can make is smaller and fitting to my screen (so I don't need to drag to see the left edge)?

Note: I'm using Xcode 12.1

UPDATE

Using Xcode 11.7, it looks better with the same code. Is this Xcode 12 issue?

enter image description here

1

There are 1 answers

1
Elye On

I worked around it using

    override func viewDidLoad() {
        imageView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)
        imageView.backgroundColor = .cyan
        view.addSubview(imageView)

        imageView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            imageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
    }