简体   繁体   English

iOS 布局在 Xcode 11 上的 iOS 13 上中断 - 未调用 traitCollectionDidChange

[英]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). TL;DR 在 Xcode 11 上,iOS 13 的某些应用程序布局被破坏(下面的操作系统很好)。 When I go to background and come back, it is fixed... except if UIUserInterfaceStyle is set to Light (!)当我 go 回到背景并返回时,它是固定的......除非 UIUserInterfaceStyle 设置为 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:我正在使用最新的 Xcode (11.2) 和 Swift 4。当然,当您使用新的 Xcode 版本时,我期待一些编译问题,但一旦这些问题得到修复,我意识到:

The app doesn't layout properly anymore on iOS 13, at least some views of the app.该应用程序在 iOS 13 上不再正确布局,至少该应用程序的某些视图。 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).虽然在 AppStore 布局上不是问题,所以我猜它是 iOS 13 + Xcode 11 (我在这里猜,我不知道使用哪个 ZA3B5EBD8A1E9EBF44A172E80D5AZ33 构建当前版本)。

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.有一个例外,如果我在应用程序的 info.plist 上启用 UIUserInterfaceStyle 以点亮,则布局永远不会自行修复。

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.如果在 peek & pop 之后您不输入并稍后正常输入,它仍然无法修复。

I have no clue how I found it, but the solution was on a post of useyourloaf我不知道我是如何找到它的,但解决方案在 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!显然 iOS 13 现在能够在加载视图之前预测特征,因此加载视图时不再调用traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)

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.有时我们需要知道设备的大小来调整一些约束,我们指望这种方法在那里(改变时),但由于 iOS 已经检测到它,特征是在加载之前设置的,并且永远不会调用该方法。 Therefore, that piece of logic should be moved to somewhere else (viewDidLoad, layoutSubviews, ...).因此,应该将那段逻辑移到其他地方(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/资料来源:https://useyourloaf.com/blog/predicting-size-classes-in-ios-13/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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