-3

除了顶部的文本字段,当我开始输入时,iphone 键盘会隐藏所有剩余的文本字段。如何在键盘上方显示活动的文本字段,然后在键盘隐藏时将其恢复正常?

4

4 回答 4

1

注意UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。与这些通知一起传递的NSNotification对象将具有 userInfo 字典,其中包含有关键盘完成动画后将具有的帧的信息。根据这些通知调整您的布局,以防止您想要的东西被键盘覆盖。

于 2013-03-19T07:23:11.233 回答
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];
}
于 2013-03-19T07:23:19.123 回答
1

放入UITextFieldUIScrollView使用如下contentOffSet属性UIScrollView

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    if (textField == txt1) //
    {
       [txt1 becomesFirstResponder];
       scrMainView.contentOffset = CGPointMake(0, 20);
    }

    else if (textField == txt2) //
    {
       [txt1 resignFirstResponder];
       [txt2 becomesFirstResponder];
       scrMainView.contentOffset = CGPointMake(0, 60);
    }
    //And so on..
    return YES;
}

希望,你会得到答案。

谢谢。

于 2013-03-19T07:24:43.747 回答
0

您必须在视图顶部使用滚动视图,并根据您选择的文本字段实现数学 contentOffset 设置。您选择的文本字段可以从委托方法中获得,偏移量的计算可以通过文本字段框架完成

于 2013-03-19T07:21:11.690 回答