I’m trying to present the top view controller as the root view controller, the whole code works except that line :
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
I get the warning :
‘keyWindow’ was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes
Then when I say :
var topController: UIViewController = UIApplication.shared.windows.first { $0.isKeyWindow }!.rootViewController!
I get :
‘windows’ was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
And then when I say:
var topController: UIViewController = UIWindowScene.windows.first { $0.isKeyWindow }!.rootViewController!
I get :
Instance member ‘windows’ cannot be used on type ‘UIWindowScene’; did you mean to use a value of this type instead?
Can somebody help me? Sorry if this sounds easy but I’m still a novice. Thanks
3
Answers
you can set/get root view controller as follow:
You can use the following code base
You can try an alternative solution using the
UIResponder
class. Climbing up the hierarchy until you reach the first element of theUIWindow
type.