首页 文章

如何获得键盘的高度?

提问于
浏览
63

不同iOS设备上键盘的高度不同 . 有人知道如何以编程方式获得设备键盘的高度吗?

7 回答

  • 16

    In Swift:

    您可以通过订阅 UIKeyboardWillShowNotification 通知来获得键盘高度 . (假设你想知道它显示之前的高度) .

    像这样的东西:

    斯威夫特2

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
    

    斯威夫特3

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(keyboardWillShow),
        name: NSNotification.Name.UIKeyboardWillShow,
        object: nil
    )
    

    斯威夫特4

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(keyboardWillShow),
        name: UIResponder.keyboardWillShowNotification,
        object: nil
    )
    

    然后你可以在 keyboardWillShow 函数中访问高度,如下所示:

    斯威夫特2

    func keyboardWillShow(notification: NSNotification) {
        let userInfo: NSDictionary = notification.userInfo!
        let keyboardFrame: NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height
    }
    

    斯威夫特3

    @objc func keyboardWillShow(_ notification: Notification) {
        if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            let keyboardHeight = keyboardRectangle.height
        }
    }
    

    斯威夫特4

    @objc func keyboardWillShow(_ notification: Notification) {
        if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
            let keyboardRectangle = keyboardFrame.cgRectValue
            let keyboardHeight = keyboardRectangle.height
        }
    
  • 53

    Swift 3.0和Swift 4.1

    1-在 viewWillAppear 方法中注册通知:

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    

    2-要求的方法:

    @objc func keyboardWillShow(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardSize.height
            print(keyboardHeight)
        }
    }
    
  • 1

    Swift 4 and Constraints

    在您的tableview中添加相对于底部安全区域的底部约束 . 在我的例子中,约束称为tableViewBottomLayoutConstraint .

    @IBOutlet weak var tableViewBottomLayoutConstraint: NSLayoutConstraint!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(notification:)), name: .UIKeyboardWillHide, object: nil)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow , object: nil)
        NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide , object: nil)
    }
    
    @objc 
    func keyboardWillAppear(notification: NSNotification?) {
    
        guard let keyboardFrame = notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
            return
        }
    
        let keyboardHeight: CGFloat
        if #available(iOS 11.0, *) {
            keyboardHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
        } else {
            keyboardHeight = keyboardFrame.cgRectValue.height
        }
    
        tableViewBottomLayoutConstraint.constant = keyboardHeight
    }
    
    @objc 
    func keyboardWillDisappear(notification: NSNotification?) {
        tableViewBottomLayoutConstraint.constant = 0.0
    }
    
  • 129

    Update Swift 4.2

    private func setUpObserver() {
        NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: UIResponder.keyboardWillShowNotification, object: nil)
    }
    

    选择器方法:

    @objc fileprivate func keyboardWillShow(notification:NSNotification) {
        if let keyboardRectValue = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardRectValue.height
        }
    }
    

    延期:

    private extension Selector {
        static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:)) 
    }
    

    Update Swift 3.0

    private func setUpObserver() {
        NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: .UIKeyboardWillShow, object: nil)
    }
    

    选择器方法:

    @objc fileprivate func keyboardWillShow(notification:NSNotification) {
        if let keyboardRectValue = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            let keyboardHeight = keyboardRectValue.height
        }
    }
    

    延期:

    private extension Selector {
        static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:)) 
    }
    

    Tip

    UIKeyboardDidShowNotification或UIKeyboardWillShowNotification可能会调用两次并获得不同的结果,这article解释了为什么调用两次 .

    In Swift 2.2

    Swift 2.2不推荐使用选择器的字符串,而是引入了新的语法: #selector .

    就像是:

    private func setUpObserver() {
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: .keyboardWillShow, name: UIKeyboardWillShowNotification, object: nil)
    }
    

    选择器方法:

    @objc private func keyboardWillShow(notification:NSNotification) {
    
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        let keyboardHeight = keyboardRectangle.height
        editorBottomCT.constant = keyboardHeight
    }
    

    延期:

    private extension Selector {
    
        static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(_:)) 
    }
    
  • 10

    更短的版本:

    func keyboardWillShow(notification: NSNotification) {
    
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                let keyboardHeight = keyboardSize.height
            }
    }
    
  • 15

    Swift 4 .

    最简单的方法

    override func viewDidLoad() {
          super.viewDidLoad()
          NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
    }
    
    func keyboardWillShow(notification: NSNotification) {  
    
          if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                 let keyboardHeight : Int = Int(keyboardSize.height)
                 print("keyboardHeight",keyboardHeight) 
          }
    
    }
    
  • 2

    //步骤1: - 注册NotificationCenter

    ViewDidLoad() {
    
       self.yourtextfield.becomefirstresponder()
    
       // Register your Notification, To know When Key Board Appears.
        NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    
       // Register your Notification, To know When Key Board Hides.
        NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    

    //步骤2: - 当键盘出现或隐藏时,将自动调用这些方法

    func keyboardWillShow(notification:NSNotification) {
            let userInfo:NSDictionary = notification.userInfo! as NSDictionary
            let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
            let keyboardRectangle = keyboardFrame.cgRectValue
            let keyboardHeight = keyboardRectangle.height
            tblViewListData.frame.size.height = fltTblHeight-keyboardHeight
        }
    
        func keyboardWillHide(notification:NSNotification) {
            tblViewListData.frame.size.height = fltTblHeight
        }
    

相关问题