【问题标题】:Adjust static UITableView when keyboard appears?出现键盘时调整静态 UITableView?
【发布时间】:2014-06-15 14:53:08
【问题描述】:

我正在开发一个包含UITextFieldUITextView 的静态UITableView 应用程序。我面临两个问题

  1. 当我选择 UITextField 时,它移动正确,但不适用于 UITextView
  2. 当键盘消失时,UITableView 未正确显示。

我想要的只是在键盘出现和消失时相应地调整UITextFieldUITextView。这是我的代码。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];




- (void)keyboardWillShow:(NSNotification *)notification {
//get the end position keyboard frame
NSDictionary *keyInfo = [notification userInfo];
CGRect keyboardFrame = [[keyInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//convert it to the same view coords as the tableView it might be occluding
keyboardFrame = [self.tableView convertRect:keyboardFrame fromView:nil];
//calculate if the rects intersect
CGRect intersect = CGRectIntersection(keyboardFrame, self.tableView.bounds);



if (!CGRectIsNull(intersect)) {
    //yes they do - adjust the insets on tableview to handle it
    //first get the duration of the keyboard appearance animation
    NSTimeInterval duration = [[keyInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    //change the table insets to match - animated to the same duration of the keyboard appearance
    [UIView animateWithDuration:duration animations:^{
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, intersect.size.height, 0);
        self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, intersect.size.height, 0);
    }];
}


}


- (void) keyboardWillHide:  (NSNotification *) notification{
NSDictionary *keyInfo = [notification userInfo];
NSTimeInterval duration = [[keyInfo objectForKey:@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
//clear the table insets - animated to the same duration of the keyboard disappearance
[UIView animateWithDuration:duration animations:^{

    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}];


}

【问题讨论】:

    标签: iphone objective-c uitableview ios7


    【解决方案1】:

    尝试使用 UITableView 的实例方法:scrollToRowAtIndexPath:atScrollPosition:animated:

    在 keyBoardWillShow 中使用 tableview 或 textfield 的索引路径并将滚动位置设置为 UITableViewScrollPositionTop

    如果它没有按您的意愿工作,请尝试使用 scrollToRowAtIndexPath:atScrollPosition:animated:

    希望这会有所帮助,祝你好运! :))

    如需更多信息,请查看 Apple 的 UITableView 参考:https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html

    【讨论】:

      【解决方案2】:

      您可以使用第三方库来避免文本字段或文本视图上方的键盘滚动。 使用https://github.com/cokecoffe/ios-demo/blob/master/TPKeyboardAvoiding/TPKeyboardAvoidingScrollView.m

      您可以使用 UITableviewController 作为您的视图控制器类。将 Tableview 内容属性更改为静态。然后,您可以从故事板将任何 UI 控件添加到表格视图的静态单元格。 如果您在表格视图中选择任何文本字段或文本视图,则键盘会自动显示在您的文本字段或文本视图下方,无需任何编程逻辑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-22
        • 2013-02-25
        • 2018-01-05
        • 2015-08-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多