Swift 不通过注册通知获取键盘高度
func getVisiableKeyboardHeight() -> CGFloat {
var keyboardWindow : UIWindow?
for window : UIWindow in UIApplication.shared.windows {
if !window.isKind(of: UIWindow.self) {
keyboardWindow = window
}
}
if keyboardWindow == nil { return 0 }
var inputView : UIView?
for testWindow : UIView in keyboardWindow!.subviews {
if testWindow.isKind(of: NSClassFromString("UIInputSetContainerView")!) {
inputView = testWindow;
break;
}
}
if inputView == nil { return 0 }
for possibleKeyboard : UIView in inputView!.subviews {
if possibleKeyboard.isKind(of: NSClassFromString("UIInputSetHostView")!) || possibleKeyboard.isKind(of: NSClassFromString("UIKeyboard")!) {
return possibleKeyboard.bounds.size.height;
}
}
return 0
}
网友评论