作者:中孝雪瑶诗涵 | 来源:互联网 | 2023-05-18 21:03
IunderstandthattheissueImpostinghasbeendiscussedalotbutasfarasIhavesearchedinSO
I understand that the issue I'm posting has been discussed a lot but as far as I have searched in SO, I couldn't find a solution specific to my issue. I'm posting this to get some inputs.
我知道我发布的问题已经讨论了很多,但就我在SO中搜索过,我找不到特定于我的问题的解决方案。我发布这个以获得一些输入。
Problem:
View hierarchy: View -> ScrollView -> View -> Textfield
查看层次结构:查看 - > ScrollView - >查看 - >文本字段
I have implemented two screens with multiple textfields laid on top of a scrollview to avoid keyboard obscuring the textfield when its at the bottom of the page. I have achieved this using the sample code provided by Apple with minor modifications.
我已经实现了两个屏幕,其中多个文本字段放在滚动视图的顶部,以避免键盘在页面底部模糊文本字段。我使用Apple提供的示例代码进行了少量修改。
- Subscribe to
UIKeyboardWillShowNotification
and UIKeyboardWillHideNotification
订阅UIKeyboardWillShowNotification和UIKeyboardWillHideNotification
- When keyboard is shown, get the height of the keyboard from
userInfo
dictionary and set appropriate content inset to move the textfield above the Keyboard
显示键盘时,从userInfo字典中获取键盘的高度并设置适当的内容插入以在键盘上方移动文本字段
- When keyboard is hidden, set the content inset to
UIEdgeInsetZero
to bring back to normal size
隐藏键盘时,将内容插入设置为UIEdgeInsetZero以恢复正常大小
The above implementation works perfectly fine when there are more textfields that could occupy the entire screen (assume there are 10 textfields and they extend beyond the frame of the the scroll view). Whenever I tap on a textfield, it moves above the keyboard as expected and I can also scroll the page till the bottom most textfield is visible above the keyboard frame (when the keyboard is still up).
当有更多的文本字段可以占据整个屏幕时,上述实现工作得非常好(假设有10个文本字段并且它们超出了滚动视图的框架)。每当我点击文本字段时,它会按预期移动到键盘上方,我也可以滚动页面,直到键盘框架上方可见最底部的文本字段(当键盘仍处于运行状态时)。
My problem arises when I have only two textfield in the scrollview that are centered vertically in the screen. When I tap on a textfield, the scrollview get an inset equivalent to the keyboard height and it moves above the keyboard as expected but when I scroll the screen, I could see a huge blank space (due to the additional inset) below the textfields.
当我在滚动视图中只有两个文本字段在屏幕中垂直居中时,我的问题就出现了。当我点击文本字段时,滚动视图得到一个等于键盘高度的插图,它按预期移动到键盘上方但当我滚动屏幕时,我可以在文本字段下方看到一个巨大的空白区域(由于附加插页)。
How can I avoid that blank space when there are only one or two textfields in the page? Should I have to write a different logic to handle that scenario? Any help would be appreciated.
如果页面中只有一个或两个文本字段,我该如何避免该空格?我是否应该编写不同的逻辑来处理这种情况?任何帮助,将不胜感激。
Below is the code:
以下是代码:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
if let kbFrame = keyboardFrame {
let inset = CGRectGetHeight(kbFrame)
scrollView.cOntentInset= UIEdgeInsetsMake(0, 0, inset, 0)
scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, inset, 0)
}
}
}
func keyboardWillHide(notfication: NSNotification) {
scrollView.cOntentInset= UIEdgeInsetsZero
scrollView.scrollIndicatorInsets = UIEdgeInsetsZero
}
2 个解决方案