当有键盘时如何在开始编辑时使UITextField向上移动?

久未见 提交于 2020-08-20 08:48:46

问题:

With the iOS SDK: 使用iOS SDK:

I have a UIView with UITextField s that bring up a keyboard. 我有一个带有UITextFieldUIViewUITextField一个键盘。 I need it to be able to: 我需要它能够:

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up 提起键盘后,允许滚动UIScrollView的内容以查看其他文本字段

  2. Automatically "jump" (by scrolling up) or shortening 自动“跳跃”(通过向上滚动)或缩短

I know that I need a UIScrollView . 我知道我需要一个UIScrollView I've tried changing the class of my UIView to a UIScrollView but I'm still unable to scroll the textboxes up or down. 我尝试将UIView的类更改为UIScrollView但仍然无法向上或向下滚动文本框。

Do I need both a UIView and a UIScrollView ? 我是否需要UIViewUIScrollView Does one go inside the other? 一个人会进入另一个人吗?

What needs to be implemented in order to automatically scroll to the active text field? 为了自动滚动到活动文本字段,需要实现什么?

Ideally as much of the setup of the components as possible will be done in Interface Builder. 理想情况下,将在Interface Builder中完成尽可能多的组件设置。 I'd like to only write code for what needs it. 我只想编写所需的代码。

Note: the UIView (or UIScrollView ) that I'm working with is brought up by a tabbar ( UITabBar ), which needs to function as normal. 注意:我正在使用的UIView (或UIScrollView )由一个标签栏( UITabBar )调出,该UITabBar需要正常运行。


Edit: I am adding the scroll bar just for when the keyboard comes up. 编辑:我只是在键盘出现时添加滚动条。 Even though it's not needed, I feel like it provides a better interface because then the user can scroll and change textboxes, for example. 即使不需要它,我也觉得它提供了一个更好的界面,因为例如,用户可以滚动和更改文本框。

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. 我可以在键盘上下移动时更改UIScrollView的帧大小的地方工作。 I'm simply using: 我只是在使用:

-(void)textFieldDidBeginEditing:(UITextField *)textField { 
    //Keyboard becomes visible
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
                     scrollView.frame.origin.y, 
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50);   //resize
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
   //keyboard will hide
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, 
       scrollView.frame.origin.y, 
     scrollView.frame.size.width,
      scrollView.frame.size.height + 215 - 50); //resize
}

However, this doesn't automatically "move up" or center the lower text fields in the visible area, which is what I would really like. 但是,这不会自动“向上移动”或将下部文本字段居中显示区域的中心,这是我真正想要的。


解决方案:

参考一: https://stackoom.com/question/4j70/当有键盘时如何在开始编辑时使UITextField向上移动
参考二: https://oldbug.net/q/4j70/How-can-I-make-a-UITextField-move-up-when-the-keyboard-is-present-on-starting-to-edit
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!