简体   繁体   中英

iOS layout breaks on iOS 13 on Xcode 11 - traitCollectionDidChange is not called

TL;DR On Xcode 11 some app layout is broken for iOS 13 (bellow OS are fine). When I go to background and come back, it is fixed... except if UIUserInterfaceStyle is set to Light (!)

I am dealing with a somehow old app that hasn't been worked on for the last year. I am using the latest Xcode (11.2) and Swift 4. Of course I was expecting some compilation issues when you use a new Xcode version, but once those were fixed I realized the following:

The app doesn't layout properly anymore on iOS 13, at least some views of the app. On the AppStore layout is not a problem though, so I guess it is something with iOS 13 + Xcode 11 (I am guessing here, I don't know which Xcode version was used to build the current app).

One more important thing, when the app goes to background, suddenly the app is able to update its constraints and after coming back layout is fixed. There is one exception, if I enabled UIUserInterfaceStyle to Light on the info.plist of the app, the layout never gets fixed by its own.

Another weird case is when you peek & pop the view, then it layouts properly (on the preview and if also the view if you you finish entering using that gesture). It will still not be fixed if after the peek & pop you don't enter and enter later normally.

I have no clue how I found it, but the solution was on a post of useyourloaf

Apparently iOS 13 is now able to predict the traits before loading the view, so traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) doesn't get called anymore when loading the view!

Sometimes we need to know the size of the device to adjust some constraints and we count on this method to do it there (on change) but since the iOS already detects it, the trait is setup before loading and that method is never called. Therefore, that piece of logic should be moved to somewhere else (viewDidLoad, layoutSubviews, ...).

This fixed it for me. And now more and more things start to make sense. I have no clue however why setting up light style will cause the device not triggering the method on background.

Source:https://useyourloaf.com/blog/predicting-size-classes-in-ios-13/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM