WishIHadThreeGuns
WishIHadThreeGuns

Reputation: 1469

NSLayoutConstraint not resizing on rotation Swift

I just want to create a scrollview in the parent view, and pin to the view in code (and then go on to create a content view that is pinned and has the same width and height).

It should be simple, but when I rotate the device the scrollview does not rotate!

let scrollView = UIScrollView(frame: view.frame)
scrollView.backgroundColor = UIColor.blue
view.addSubview(scrollView)

NSLayoutConstraint.activate([
    scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
    scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
    scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
    scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
    ])

I also tried

scrollView.leadingAnchor.constraint(equalTo: scrollView.superview!.leftAnchor)

and various other combinations.

This is all called from within

viewDidLoad

If I start off in portrait and rotate to landscape (the image is on the view, the blue is the scrollview)

enter image description here

Upvotes: 0

Views: 29

Answers (1)

RajeshKumar R
RajeshKumar R

Reputation: 15758

You should assign translatesAutoresizingMaskIntoConstraints property to false

let scrollView = UIScrollView(frame: view.frame)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = UIColor.blue
view.addSubview(scrollView)

Upvotes: 1

Related Questions