2

我正在尝试在多场景应用程序中实现 ios13 暗模式。

不幸的是,当我关闭一个将其拖过屏幕边缘的场景时,traitCollectionDidChange 方法会被多次调用,并且总是使用不同的值,导致我的 UI 在暗模式和亮模式之间闪烁。

怎么了?

这是我的实现

func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {  
    super.traitCollectionDidChange(previousTraitCollection)  

    print("THEME instance: \(self)")  

    let currentTraitCollection = self.traitCollection  
    var hasUserInterfaceStyleChanged = false  
    hasUserInterfaceStyleChanged = previousTraitCollection.hasDifferentColorAppearanceCompared(to: currentTraitCollection)  

    print("THEME hasUserInterfaceStyleChanged = \(hasUserInterfaceStyleChanged ? "YES" : "NO")")  

    if hasUserInterfaceStyleChanged {  
        let userInterfaceStyle = currentTraitCollection.userInterfaceStyle // Either .unspecified, .light, or .dark  

        switch userInterfaceStyle {  
            case .unspecified:  
                print("THEME UIUserInterfaceStyleUnspecified")  
            case .light:  
                print("THEME UIUserInterfaceStyleLight")  
            case .dark:  
                print("THEME UIUserInterfaceStyleDark")  
            }  
    } else {  
        print("THEME NOT CHANGED")  
    }  

}  

这是控制台中记录的语句

当新场景出现时...

THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = YES  
THEME UIUserInterfaceStyleLight  

当添加的场景消失时......

THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = YES  
THEME UIUserInterfaceStyleDark  
THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = YES  
THEME UIUserInterfaceStyleLight  
THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = NO  
THEME NOT CHANGED  
THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = YES  
THEME UIUserInterfaceStyleDark  
THEME instance: <MainControllerViewController: 0x117e55910>  
THEME hasUserInterfaceStyleChanged = YES  
THEME UIUserInterfaceStyleLight  

与此同时,我没有更改为暗模式(总是亮)......所以我希望只是主题没有改变。

4

1 回答 1

2

我在同一个问题上苦苦挣扎,我制定的解决方案在SceneDelegate.

AUIScene有多个状态:

.foregroundActive
.foregroundInactive
.background
.unattached

当您在幻灯片中调整窗口大小时,或者在这种情况下,从幻灯片中删除一个窗口时,traitCollectionDidChange会为每个窗口调用。这意味着您正在更新、和状态userInterfaceStyle中的场景。这就是导致闪烁的原因。.background.foregroundInactive.unattached

解决方案是不使用traitCollectionDidChange,而是在SceneDelegate调用中使用委托方法windowScene(_:didUpdate:interfaceOrientation:traitCollection:)

根据Apple 的文档,此方法:

当场景的大小、方向或特征发生变化时通知您。

这样做的额外好处是我们可以.activationState在更新userInterfaceStyle.

    func windowScene(_ windowScene: UIWindowScene, didUpdate previousCoordinateSpace: UICoordinateSpace, interfaceOrientation previousInterfaceOrientation: UIInterfaceOrientation, traitCollection previousTraitCollection: UITraitCollection) {

          let currentTraitCollection = windowScene.traitCollection

          if windowScene.activationState == .foregroundActive {
             if currentTraitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle {
                 if currentTraitCollection.userInterfaceStyle == .light {
                     //update to light theme
                 } else {
                     //update to dark theme
                 }
             }
         }
     }
于 2020-05-15T13:42:09.400 回答