Use overrideUserInterfaceStyle on all active windows

387 views Asked by At

I have appearance settings in my app that look like that:

func changeInterfaceStyle(to mode: UIUserInterfaceStyle) {
    guard let window = view.window else { return }
    UIView.transition(with: window, duration: 0.5, options: .transitionCrossDissolve) {
        window.overrideUserInterfaceStyle = mode
    }
}

And obviously I want it to apply even if multiple instances of the app are open simultaneously on an iPad.
I've seen many things about keyWindows, UIWindowScene.connectedScene, .foregroundActive here and there, but not some up-to-date, efficient and for multiple windows code.

So my question is simple, what's the best way to loop over all active windows and efficiently override their interface style? (btw my app is iOS 14+)

EDIT 1:
Ugly workaround:

UIApplication.shared.connectedScenes.filter { $0 is UIWindowScene }.compactMap { ($0 as? UIWindowScene)?.windows as? [UIWindow] }.reduce([], +).filter { $0.isOpaque }.forEach { win in
    UIView.transition(with: win, duration: 0.5, options: .transitionCrossDissolve) {
        win.overrideUserInterfaceStyle = mode
    }
}
0

There are 0 answers