当键盘存在时,如何让UITextField向上移动? [英] How can I make a UITextField move up when the keyboard is present?

查看:105
本文介绍了当键盘存在时,如何让UITextField向上移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用iOS SDK:

我有一个 UIView ,带有 UITextField s调出键盘。我需要它能够:

I have a UIView with UITextFields that bring up a keyboard. I need it to be able to:


  1. 允许滚动 UIScrollView 在键盘启动后查看其他文本字段

  1. Allow scrolling of the contents of the UIScrollView to see the other text fields once the keyboard is brought up

自动跳转(通过向上滚动)或缩短

Automatically "jump" (by scrolling up) or shortening

我知道我需要一个 UIScrollView 。我已经尝试将我的 UIView 的类更改为 UIScrollView 但我仍然无法向上滚动文本框或者下来。

I know that I need a 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? Does one go inside the other?

为了自动滚动到活动文本字段需要实现什么?

What needs to be implemented in order to automatically scroll to the active text field?

理想情况下,尽可能多的组件设置将在Interface Builder中完成。我只想编写需要它的代码。

Ideally as much of the setup of the components as possible will be done in Interface Builder. I'd like to only write code for what needs it.

注意: UIView (或<$ c我正在使用的$ c> UIScrollView )是由一个标签栏( UITabBar )启动的,它需要正常运行。

Note: the UIView (or UIScrollView) that I'm working with is brought up by a tabbar (UITabBar), which needs to function as normal.

编辑:我正在添加滚动条,仅用于键盘出现时。即使它不需要,我觉得它提供了更好的界面,因为用户可以滚动和更改文本框。

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.

当键盘上下变化时我改变了 UIScrollView 的帧大小。我只是使用:

I've got it working where I change the frame size of the UIScrollView when the keyboard goes up and down. 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.

推荐答案


  1. 如果你现在的内容不适合iPhone屏幕,你只需要一个 ScrollView
    (如果你要添加 ScrollView 作为组件的超级视图。只需要 TextField 滚动当键盘出现时,则不需要它。)

  1. You will only need a ScrollView if the contents you have now do not fit in the iPhone screen. (If you are adding the ScrollView as the superview of the components. just to make the TextField scroll up when keyboard comes up, then it's not needed.)

用于显示 textfields 而不隐藏通过键盘,标准方法是在显示键盘时向上/向下移动包含文本字段的视图。

For showing the textfields without being hidden by the keyboard, the standard way is to move up/down the view having textfields whenever the keyboard is shown.

下面是一些示例代码:

#define kOFFSET_FOR_KEYBOARD 80.0

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
    if ([sender isEqual:mailTf])
    {
        //move the main view, so that the keyboard does not hide it.
        if  (self.view.frame.origin.y >= 0)
        {
            [self setViewMovedUp:YES];
        }
    }
}

//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view

    CGRect rect = self.view.frame;
    if (movedUp)
    {
        // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
        // 2. increase the size of the view so that the area behind the keyboard is covered up.
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
        rect.size.height += kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        // revert back to the normal state.
        rect.origin.y += kOFFSET_FOR_KEYBOARD;
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
    }
    self.view.frame = rect;

    [UIView commitAnimations];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillHideNotification
                                           object:nil];
}

这篇关于当键盘存在时,如何让UITextField向上移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆