【问题标题】:iOS no height for keyboard as notification data is nulliOS没有键盘高度,因为通知数据为空
【发布时间】:2018-11-23 15:10:39
【问题描述】:

我一直致力于在 swift 4.2 中获得键盘的高度。当键盘显示哪个接受通知作为参数时,我一直在使用通知中心调用一个函数,我认为这很标准?在这里我要计算高度。但是通知对象缺少数据,我无法计算高度。

NSConcreteNotification 0x16e43a710 {
    name = UIKeyboardDidShowNotification; userInfo = {
       UIKeyboardAnimationCurveUserInfoKey = 7;
       UIKeyboardAnimationDurationUserInfoKey = "0.25";
       UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardCenterBeginUserInfoKey = "NSPoint: {0, 0}";
       UIKeyboardCenterEndUserInfoKey = "NSPoint: {0, 0}";
       UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 0}, {0, 0}}";
       UIKeyboardIsLocalUserInfoKey = 1;
    }
}

我从我的 init 方法中调用了一个 commonInit 函数,该函数设置了要调用的方法:

private func commonInit() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardDidShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

}

KeyboardWillShow 功能

@objc private func keyboardWillShow(notification:NSNotification) {

    let userInfo = notification.userInfo! as NSDictionary
    let keyboardFrame = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
    let keyboardRectangle = keyboardFrame.cgRectValue
    let keyboardHeight = keyboardRectangle.height

    print(keyboardHeight)
}

是否缺少获取通知数据的内容?

【问题讨论】:

  • 您是在模拟器还是实际设备中尝试?如果您在模拟器中尝试,请按 Command+K 打开键盘。键盘出现了吗?
  • 我在模拟器和设备上都试过了。键盘出现在屏幕上,但我没有获得足够的通知数据来计算高度。通知对象每次都是一样的
  • 只计算键盘高度?

标签: ios iphone swift swift4.2


【解决方案1】:

这是我一直使用的:

func addObservers() {
        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillShowNotification,
                                               object: nil,
                                               queue: nil) { [weak self] (notification) in
                                                self?.keyboardWillShow(notification: notification as NSNotification)
        }

        NotificationCenter.default.addObserver(forName: UIResponder.keyboardWillHideNotification,
                                               object: nil,
                                               queue: nil) {  [weak self] (notification) in
                                                self?.keyboardWillHide(notification: notification as NSNotification)
        }

    }


func keyboardWillShow(notification: NSNotification) {
        if let userInfo = notification.userInfo {
            let keyboardHeight = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as AnyObject).cgRectValue.size.height
     }
     //... do your stuff...
 }

【讨论】:

  • 通知不包含获取高度的数据。由于某种原因,通知缺少允许计算高度的数据
  • 这个答案有什么帮助? OP 已经表明他正在使用 keyboardWillShowNotification 并且 keyboardFrameEndUserInfoKey 包含零矩形。
  • @KeithDarragh:你什么时候调用 commonInit() 函数?会不会是 VC 生命周期中的问题?我通常在 viewWillAppear 函数中调用
  • 它没有在 UIViewController 中被调用 - 它在 UIView 中被调用。所以它在 super.init(frame: CGRect.zero) 之后在它的 init 函数中被调用。这种类型的通知在哪里调用是否重要?
  • MMmm...即使在 UIView 中它也可以工作,我已经尝试过代码。能否请您显示整个代码?
猜你喜欢
  • 2018-01-23
  • 2011-10-29
  • 2019-05-23
  • 2015-12-04
  • 2015-03-05
  • 2015-11-14
  • 1970-01-01
  • 1970-01-01
  • 2014-11-24
相关资源
最近更新 更多