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

问题:

With the iOS SDK: 使用iOS SDK: ide

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

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

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

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但仍然没法向上或向下滚动文本框。 .net

Do I need both a UIView and a UIScrollView ? 我是否须要UIViewUIScrollView Does one go inside the other? 一我的会进入另外一我的吗? code

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

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. 我只想编写所需的代码。 orm

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须要正常运行。 get


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. 即便不须要它,我也以为它提供了一个更好的界面,由于例如,用户能够滚动和更改文本框。 it

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
相关文章
相关标签/搜索