swift获取keywindow

let keyWindow:UIWindow = {
    var window:UIWindow = UIApplication.shared.keyWindow!
    if #available(iOS 13.0, *) {
        window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last!
    }
    return window
}()

ios13之后获取

let window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last

或者

let window = UIApplication.shared.connectedScenes
    .filter({ $0.activationState == .foregroundActive })
    .map({ $0 as? UIWindowScene })
    .compactMap({ $0 })
    .last?.windows
    .filter({ $0.isKeyWindow })
    .last

你可能感兴趣的:(swift获取keywindow)