Swift 3.0: text column width does not always update when rotating device

Viewed 23

I have recently inherited an an iOS app made in Swift 3.0 from a developer that no longer works here.

The app is made in Xcode and uses storyboards for some of the screens, but not all of them.

On a iPad in landscape orientation the main screen contains an image taking op 2/3 of the width, with a text column next to it taking up 1/3 of the screen. Below are three images each up 1/3 wide. On smaller screens all these items take up 100% of the available width and are displayed underneath each other as a long list.

This is done using the following code in MainController.swift:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews();
    // On small screens, columnify the layout
    let mainContent = view.viewWithTag(1) as! UIStackView; //contains large image and text
    let thumbnails = view.viewWithTag(3) as! UIStackView; //three images
    mainContent.axis = .horizontal
    mainContent.distribution = .fillProportionally
    thumbnails.axis = .horizontal
    if(self.view.bounds.width < 1000) {
        mainContent.axis = .vertical
        mainContent.distribution = .equalSpacing
       thumbnails.axis = .vertical
    } else {
        mainContent.axis = .horizontal
        mainContent.distribution = .fillProportionally
       thumbnails.axis = .horizontal
    }
}

This all works well, unless the user performs the following actions:

  1. Start in landscape orientation on iPad. Image is 2/3, text 1/3
  2. Tap on thumbnail and navigate to a different panel.
  3. Rotate iPad to portrait.
  4. Navigate back to first panel.
  5. Observe that all items are places underneath each other, the image is 100%, but text is only 1/3 of the screen, while it should be 100%
  6. Rotate to landscape, text moves next to image.
  7. Rotate to portrait, text now spans 100% (as it should be)

My hunch would be that manually triggering a re-layout after navigating back to the first panel would solve it, but I cannot find code related to the navigation. This seems all handled by some "Apple magic?". There is probably a way to hook into it, but without any code I don't have any pointers. My only other solution would be to try and refactor the entire application with storyboards, but before I start with that, I was hoping on getting some insights here.

0 Answers
Related