美文网首页
2024-08-30

2024-08-30

作者: 跃文 | 来源:发表于2024-08-29 17:06 被阅读0次

    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
    }
    

    相关文章

      网友评论

          本文标题:2024-08-30

          本文链接:https://www.haomeiwen.com/subject/ynmxljtx.html