Reputation: 67
UIScreen.main.scale
will deprecated.
here it the document in UIKit.UIScreen
@available(iOS, introduced: 2.0, deprecated: 100000, message: "Use a UIScreen instance found through context instead: i.e, view.window.windowScene.screen")
open class var main: UIScreen { get } // the device's internal screen
deprecated: 100000
means to be deprecated.
So my question is how do I replace this method?
use below method can work in main App, but UIApplication.shared
doesn't available in extension app. Is there any solution to resolve this problem
@available(iOSApplicationExtension, unavailable)
extension UIWindow {
static var current: UIWindow? {
for scene in UIApplication.shared.connectedScenes {
guard let windowScene = scene as? UIWindowScene else { continue }
for window in windowScene.windows {
if window.isKeyWindow { return window }
}
}
return nil
}
}
@available(iOSApplicationExtension, unavailable)
extension UIScreen {
static var current: UIScreen? {
UIWindow.current?.screen
}
}
Upvotes: -1
Views: 1379
Reputation: 1359
extension UIViewController {
func screen() -> UIScreen? {
var parent = self.parent
var lastParent = parent
while parent != nil {
lastParent = parent
parent = parent!.parent
}
return lastParent?.view.window?.windowScene?.screen
}
}
and use this extension in
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let screen = windowScene.screen
check this link for reference Correct way to get the screen size on iOS, after UIScreen.main deprecation
Upvotes: 1