【问题标题】:slide form up when keyboard appears出现键盘时向上滑动表格
【发布时间】:2011-05-05 05:38:11
【问题描述】:

您好,我正在使用表格进行数据输入, 一些文本字段位于表单的底部。 当我单击文本字段进行写作时,键盘会出现并隐藏它后面的字段。如果我让文本字段第一响应者隐藏键盘,但这样做我无法做到这一点。 我想知道当键盘出现时,整个表单应该如何向上移动,就像我的最后一个字段出现在键盘的操作上一样 提前致谢

【问题讨论】:

标签: objective-c cocoa-touch


【解决方案1】:

在滚动视图中添加所有视图并设置滚动视图和文本字段的委托,然后使用这些委托和方法 -

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

   [self scrollViewToCenterOfScreen:textField];     
    return YES;

}

-(BOOL) textFieldShouldReturn:(UITextField *)textField{

    if ([textField isEqual:txtField1])
    {
        [txtField2 becomeFirstResponder];
    }
    else if ([textField isEqual:txtField2])
    {
        [txtField3 becomeFirstResponder];
    }
    else 
    {
        [textField resignFirstResponder];       
        [scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }

    return YES;

}

- (void)scrollViewToCenterOfScreen:(UIView *)theView {  
    CGFloat viewCenterY = theView.center.y;  
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];  

    CGFloat availableHeight = applicationFrame.size.height - 200;            // Remove area covered by keyboard  

    CGFloat y = viewCenterY - availableHeight / 2.0;  
    if (y < 0) {  
        y = 0;  
    }  
    [scrollView setContentOffset:CGPointMake(0, y) animated:YES];  

}

【讨论】:

    【解决方案2】:

    您可以使用多种策略来执行此操作——其中大多数涉及将您的视图包装在 UIScrollView 中,并在获得焦点时滚动到适当的控件。您可以在此处找到有关如何执行此操作的 Apple 文档:http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

    【讨论】:

      【解决方案3】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多