Search code examples
iosswiftuiscrollviewautolayout

Scrollview Height Changes when Alert Controller is presented


I have a scrollview with an image and text. The scrollview correctly displays its height encapsulating the image and text. When an alert controller is presented to the screen and dismissed, the scrollview's height changes. Any thoughts on why this happens and how to fix this?

enter image description here

View heirarchy before alert is presented:

enter image description here

After alert is presented:

enter image description here

The view is added after viewDidLoad and programmatically using constraints:

let offlineView = OfflineView()
view.addSubview(offlineView)
offlineView.translatesAutoresizingMaskIntoConstraints = false
offlineView.topAnchor.constraint(equalTo: navBar.bottomAnchor, constant: 0).isActive = true
offlineView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 0).isActive = true
offlineView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0).isActive = true
offlineView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0).isActive = true

OfflineView Xib constraints:

enter image description here


Solution

  • My label in the scrollview constraints were Center X axis and Static width.

    When I changed the label constraints to leading to superview = 45, trailing to superview >= 45, the error no longer persisted.

    I don't know why/how this happened, but it fixed my problem.. If anyone could explain this phenomenon.