编辑时无法正常工作时滚动视图

时间:2011-04-01 08:40:31

标签: iphone sdk keyboard uiscrollview uitextfield

我正在尝试在用户开始编辑UITextField时创建UIScrollView滚动,并且键盘隐藏了文本字段。我正在使用以下主题中的示例。

How to make a UITextField move up when keyboard is present

我的视图中有四个UITextField。第一次显示键盘时,视图不会自动滚动。如果我单击显示键盘的另一个文本字段,UIScrollView将按预期滚动。 隐藏键盘(通过点击“完成”按钮)并再次点击UITextField会出现同样的问题:UIScrollView最初不会滚动,但是当将焦点更改为另一个文本字段时,它会完美滚动。

有人可以帮助我吗?

viewDidLoad中,我设置了scrollView的大小

keyboardIsShown = NO;
CGSize scrollContentSize = CGSizeMake(320, 350);
self.scrollView.contentSize = scrollContentSize;

我在viewWillAppear

注册了键盘通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window];

然后我在viewWillDisappear

取消注册
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

通知会调用以下两种方法。

- (void)keyboardWillShow:(NSNotification *)n {
    if (keyboardIsShown) {
        return;
    }

    NSDictionary *userInfo = [n userInfo];

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [boundsValue CGRectValue].size;

    CGRect viewFrame = self.scrollView.frame;
    viewFrame.size.height -= (keyboardSize.height - 50);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:0.3];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = YES;
}

- (void)keyboardWillHide:(NSNotification *)n {
    NSDictionary *userInfo = [n userInfo];

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [boundsValue CGRectValue].size;

    CGRect viewFrame = self.scrollView.frame;
    viewFrame.size.height += (keyboardSize.height - 50);

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];

    [UIView setAnimationDuration:0.3];
    [self.scrollView setFrame:viewFrame];
    [UIView commitAnimations];

    keyboardIsShown = NO;
}

1 个答案:

答案 0 :(得分:1)

如果要在键盘可见时显示textfeild,请使用下面的代码。不要使用scrollview。如果必须使用scrollView,则忽略此答案。



#define kOFFSET_FOR_KEYBOARD 280.0

- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMoveUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMoveUp:YES];
}


- (void)textFieldDidBeginEditing:(UITextField *)textField {
    stayup = YES;
    [self setViewMoveUp:YES];
}


- (void)textFieldDidEndEditing:(UITextField *)textField {
    stayup = NO;
    [self setViewMoveUp:NO];
}

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

    CGRect rect = self.view.frame;
    if (moveUp)
    {
        // 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.

        if (rect.origin.y == 0 ) {
            rect.origin.y -= kOFFSET_FOR_KEYBOARD;
            //rect.size.height += kOFFSET_FOR_KEYBOARD;
        }

    }
    else
    {
        if (stayup == NO) {
            rect.origin.y += kOFFSET_FOR_KEYBOARD;
            //rect.size.height -= kOFFSET_FOR_KEYBOARD;
        }
    }
    self.view.frame = rect; 
    [UIView commitAnimations];
}

尝试这种方法。根据您的要求进行编辑。

相关问题