ios – 键盘存在时如何使UITextField向上移动?

使用iPhone SDK:

我有一个UIViewUITextFields这带来了一个键盘。我需要它能够:

  1. UIScrollView键盘弹出后,允许滚动浏览其他文本字段的内容
  2. 自动“跳转”(通过滚动)或缩短

我知道我需要一个UIScrollView。我尝试将我的课程UIView改为a,UIScrollView但我仍然无法向上或向下滚动文本框。

我是否需要UIView和UIScrollView?一个人进入另一个吗?[编辑:我现在知道你想要一个UIView内部UIScrollView,而诀窍是以编程方式将内容大小设置UIScrollView为该帧的大小UIView。]

那么需要实现什么才能自动滚动到活动文本字段?

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

注意:我正在使用的UIView(或UIScrollView)是由一个tabbar(UITabBar)提供的,它需要正常运行。


编辑:我刚刚添加滚动条,当键盘出现时。尽管不需要,但我觉得它提供了更好的界面,例如,用户可以滚动和更改文本框。

我有它的工作,我改变UIScrollView了键盘上下的框架大小。我只是简单地使用:

-(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
}

然而,这并不会自动“向上移动”或将可见区域中的较低文本字段居中,这正是我真正想要的。

答案


    1. 你只需要一个,ScrollView如果你现在的内容不适合在iPhone屏幕上。(如果您添加了ScrollView组件的超级视图,只需TextField在键盘出现时向上滚动,则不需要)。
    2. 为了显示textfields没有被键盘隐藏的情况,标准方法是在显示键盘时向上/向下移动具有文本字段的视图。

    以下是一些示例代码:

    #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];

添加评论

友情链接:蝴蝶教程