【发布时间】:2016-05-24 19:04:20
【问题描述】:
我正在使用 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 按钮时,滚动视图中断。
有人可以帮忙吗?如果您需要更多代码,请告诉我
【问题讨论】:
-
在键盘完全关闭后尝试调用
displayAlert()函数。
标签: ios iphone swift uiscrollview uialertview