键盘隐藏UIScrollView中的文本字段。如何根据键盘移动文本字段?

时间:2012-10-03 17:02:39

标签: iphone objective-c ios xcode

我正在开发一个包含15个文本字段的应用。所以我使用UIScrollView使滚动成为可能。但是,当单击要编辑的文本字段时,我的最后五个文本字段隐藏在键盘后面。如何在编辑模式下将这些文本字段移动到键盘上方?同样,它们的文本字段位于UIScrollView上。不是UIView。

4 个答案:

答案 0 :(得分:2)

尝试TPKeyboardAvoidingScrollView

只需抓住您已有的滚动视图并将其类更改为TPKeyboardAvoidingScrollView,现在当键盘弹出时,它将调整其大小和偏移量,以便正确显示这些字段。

我推荐这个,因为当你有复杂的布局(许多文本字段和其他组件)时,手动执行此操作可能会非常棘手。这个控件很简单,就像一个魅力!

答案 1 :(得分:1)

以下是一些示例代码:

#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
{
    // 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
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

答案 2 :(得分:0)

您需要在scrollView上相应地设置contentSize和contentOffset。因此,如果您的上一个文本框位于(0,300)的原点,您可能希望您的contentSize为CGSizeMake(0,600)左右,然后将contentOffset设置为(0,250)。

答案 3 :(得分:0)

您可以减少scrollView

中的textFieldShouldBeginEditing: Y 坐标
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   if(textField == textField11 || textField == textField12 || textField == textField13 || textField == textField14 || textField == textField15)
   {   
       [UIView beginAnimations:nil context:NULL];
       [UIView setAnimationDuration:0.3];
       //set Y according to keyBoard height
       [scrollView setFrame:CGRectMake(0.0,-220.0,320.0,460.0)];
       [UIView commitAnimations];
   }
}

并设置按{strong>返回键的键盘时的scrollView框架

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    [scrollView setFrame:CGRectMake(0.0,0.0,320.0,460.0)];
    [UIView commitAnimations];   
}