【问题标题】:How to make the screen scroll while using the iPhone keypad如何在使用 iPhone 键盘时使屏幕滚动
【发布时间】:2011-07-25 11:12:19
【问题描述】:

我正在开发一个横向视图的应用程序。我正在使用一些 UITextFields,双击它可以让您编辑 TextFields。我的问题是如何让屏幕滚动,以便用户可以在显示键盘时编辑整个屏幕?

【问题讨论】:

标签: iphone ios4 iphone-sdk-3.0


【解决方案1】:

使用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;
}

【讨论】:

  • UIView *responder = ? tex2...你能举个例子吗?
  • 只需执行UIView *responder = theTextField,其中theTextField 是您的文本字段。原因在于下一行获取视图的大小(框架)。
【解决方案2】:

您可以使用 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];

} `

【讨论】:

    【解决方案3】:

    您可以在link 上参考解决方案。基本上,您需要按数量移动滚动视图,以便您的文本字段可见。在你的 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];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      相关资源
      最近更新 更多