如何在使用iPhone键盘时滚动屏幕

时间:2011-07-25 11:12:20

标签: iphone ios4 iphone-sdk-3.0

我正在处理横向视图中的应用。我正在使用一些UITextFields,当双击它时,它将使您能够编辑TextFields。我的问题是如何让屏幕滚动,这样用户可以在键盘显示时编辑整个屏幕?

3 个答案:

答案 0 :(得分:0)

您可以使用uitextfield委托方法。

`- (void)textFieldDidBeginEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y+60);
    [UIView commitAnimations];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
            [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    self.view.center=CGPointMake(self.view.center.x, self.view.center.y-60);
    [UIView commitAnimations];

}   `

答案 1 :(得分:0)

您可以在此link引用解决方案。基本上,您需要按金额移动滚动视图,以便显示文本字段。在yout textfieldbeginediting中调用下面的方法。

- (void)scrollViewToCenterOfScreen:(UIView *)theView {
    CGFloat viewCenterY = theView.center.y;
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    CGRect keyboardBounds = CGRectMake(0, 280, 320, 200);
    CGFloat availableHeight = applicationFrame.size.height - keyboardBounds.size.height;    // Remove area covered by keyboard

    CGFloat y = viewCenterY - availableHeight / 2.0;
    if (y < 0) {
        y = 0;
    }
    scrollview.contentSize = CGSizeMake(applicationFrame.size.width, applicationFrame.size.height + keyboardBounds.size.height);
    [scrollview setContentOffset:CGPointMake(0, y) animated:YES];
}

答案 2 :(得分:0)

使用contentInsetscrollRectToVisible对我很有帮助。下面的代码将滚动视图保护为不受键盘覆盖,然后滚动内容以显示文本字段。

- (void)keyboardWillShow:(NSNotification *)aNotification 
{
    CGRect kbFrame;
    [[aNotification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&kbFrame];                    
    float kbHeight = [self convertRect:kbFrame fromView:nil].size.height;               
    float d = kbHeight - self.frame.origin.y / self.transform.a;        
    d = d < 0 ? 0 : d;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, d, 0.0);

    self.contentInset = contentInsets;
    self.scrollIndicatorInsets = contentInsets;

    UIView *responder = /* ... your text field ... */
    [self scrollRectToVisible:responder.frame animated:YES];        
    [self performSelector:@selector(flashScrollIndicators) withObject:nil afterDelay:0.0];
}

- (void)keyboardWillHide:(NSNotification *)aNotification 
{
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;

    [[aNotification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];

    [UIView animateWithDuration:animationDuration 
                          delay:0 
                        options:animationCurve
                     animations:^{
                         self.contentInset = UIEdgeInsetsZero;
                         self.scrollIndicatorInsets = UIEdgeInsetsZero;                      
                     } 
                     completion:nil];

    [self setContentOffset:CGPointMake(0, 0) animated:YES]; 
    self.scrollEnabled = NO;
}
相关问题