繁体   English   中英

UIKeyboardWillShowNotification和UIAlert

[英]UIKeyboardWillShowNotification and UIAlert

我正在使用UIKeyboardWillShowNotification在调用键盘时上下滚动视图。 这在大多数情况下都可以正常工作。 但是,键盘具有完成按钮,可以产生UIAlert。 如果没有UIAlert,就不会有问题,但是如果调用UIAlert,则滚动视图会发生奇怪的事情,似乎会停止工作,因为它的大小会变小。

这是我正在使用的代码:

    func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
    guard let value = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
    let keyboardFrame = value.CGRectValue()
    let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 70) * (show ? 1 : -1)


    scrollView.contentInset.bottom += adjustmentHeight
    //scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}

func keyboardWillShow(notification: NSNotification) {
    if keyboardVisible == false {
    adjustInsetForKeyboardShow(true, notification: notification)
    keyboardVisible = true
    }
}

func keyboardWillHide(notification: NSNotification) {
    adjustInsetForKeyboardShow(false, notification: notification)
    keyboardVisible = false
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

键盘上就会有一个包含以下代码的按钮:

func displayAlert(title:String, message:String, view:UIViewController){
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) -> Void in
    }))
    view.presentViewController(alert, animated: true, completion: nil)
}

结果是发出警报,然后当我按OK按钮时,滚动视图中断。

有人可以帮忙吗? 让我知道您是否需要更多代码

如果可能的话,我首先建议您使用表格视图而不是滚动视图。 其次,我不知道您是否进行了测试,但是这些通知被多次调用,并且有时无法按预期运行。 我没有尝试过,但是我假设一旦显示UIAlert,就会触发其中一种方法,然后您的内容大小就会变得疯狂。 尝试设置断点,看看会发生什么。 另外,尝试在返回时关闭键盘,然后调用displayAlert()。 另外,从经验来看,从屏幕上移出时不会调用删除观察者的deinit方法,我不知道您是否有理由使用它? 最好使用viewWillAppear,viewWillDissapear方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM