gpt4 book ai didi

ios - KeyboardWillShowNotification 和 UIAlert

转载 作者:行者123 更新时间:2023-11-30 13:24:07 25 4
gpt4 key购买 nike

当调用键盘时,我使用 UIKeyboardWillShowNotification 上下 ScrollView 。这在大多数情况下都可以正常工作。但是,键盘有一个完成按钮,可以生成 UIAlert。没有 UIAlert 没有问题,但是如果调用 UIAlert , ScrollView 会发生一些奇怪的事情,它似乎会停止工作,因为它的大小变得更小。

这是我正在使用的代码:

    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)
}

结果是发出警报,然后当我按下“确定”按钮时, ScrollView 会中断。

有人可以帮忙吗?如果您需要更多代码,请告诉我

最佳答案

如果可能的话,我首先建议您使用表格 View 而不是 ScrollView 。其次,我不知道您是否测试过,但这些通知被多次调用,并且有时它们的行为与您的预期不同。我还没有尝试过,但我假设一旦您显示 UIAlert,这些方法之一就会被触发,然后您的内容大小就会变得疯狂。尝试设置断点,看看会发生什么。另外,尝试在返回时关闭键盘,然后调用 displayAlert()。另外,根据经验,当您离开屏幕时,删除观察者的 deinit 方法不会被调用,我不知道您是否有使用它的理由?最好使用 viewWillAppear、viewWillDissapear 方法。

关于ios - KeyboardWillShowNotification 和 UIAlert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37421860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com