gpt4 book ai didi

swift - Scrollview 滚动不适用于隐藏键盘?

转载 作者:行者123 更新时间:2023-11-30 11:36:00 28 4
gpt4 key购买 nike

我有一个带有一些文本字段的 ScrollView 。滚动一开始很好,但隐藏键盘后,尽管 contentView 大于scrollView,滚动不起作用。我使用以下函数来显示和隐藏键盘:

func keyboardWasShown(notification: NSNotification){
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets

var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let activeField = self.activeField {
if (!aRect.contains(activeField.frame.origin)){
self.scrollView.scrollRectToVisible(activeField.frame, animated: true)
}
}
}

func keyboardWillBeHidden(notification: NSNotification){
//Once keyboard disappears, restore original positions
var info = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.isScrollEnabled = true
}

最佳答案

如果您想关闭键盘,请将 UIKeyboardFrameBeginUserInfoKey 更改为 UIKeyboardFrameEndUserInfoKey

关于swift - Scrollview 滚动不适用于隐藏键盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49767614/

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