UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to create a value type from UIWindowScene?

Forums > Swift

In iOS15, I receive the following warning:

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

When I try to use UIWindowScene, I am warned that I need to use a value type. But however, I get init() not available when I try to do that.

Here's my use case below

func overrideDisplayMode() {
    var userInterfaceStyle: UIUserInterfaceStyle
    let overrideUserInterfaceStyle = UIWindowScene()  // init() not available

    switch displayMode {
    case .dark:
        userInterfaceStyle = .dark
    case .light:
        userInterfaceStyle = .light
    case .system:
        userInterfaceStyle = UITraitCollection.current.userInterfaceStyle
    }

    UIWindowScene.windows.first?.overrideUserInterfaceStyle = userInterfaceStyle // replacement for below, but I get error for not using a value type

    UIApplication.shared.windows.first?.overrideUserInterfaceStyle = userInterfaceStyle // deprecated
}

How to fix this?

3      

Try this:

let scenes = UIApplication.shared.connectedScenes
let windowScenes = scenes.first as? UIWindowScene
let window = windowScenes?.windows.first

4      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.