iOS 13.0废弃'keyWindow'的解决方法

时间:2022-06-01 16:42:28

使用In Xcode 11 beta 4,提示keyWindow已经被iOS 13.0废弃:

'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

解决方法

方法一:

UIApplication.shared.windows.filter {$0.isKeyWindow}.first

或者:

UIApplication.shared.windows.first { $0.isKeyWindow }

方法二:

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

使用:

keyWindow?.endEditing(true)