TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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.