How to get UIKeyboard size with iOS

前端 未结 4 1714
后悔当初
后悔当初 2020-11-27 12:35

Is there some way to get UIKeyboard size programmatically. 216.0f height and 162.0f height in landscape.

Following seem to be deprecated. Is there some way that work

4条回答
  •  旧巷少年郎
    2020-11-27 12:42

    On swift 4

    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(getInfo(notif:)), name: .UIKeyboardDidShow , object: nil)
    }
    

    and then:

    @objc func getInfo(notif: NSNotification) -> Void {  
        guard let userInfo = notif.userInfo else {return}
    
        if let myData = userInfo["UIKeyboardFrameBeginUserInfoKey"] as? CGRect {
            print(myData.width)
            print(myData.height)
        }
    }
    

提交回复
热议问题