【问题标题】:ContentInsets doesn't work for UITableViewContentInsets 不适用于 UITableView
【发布时间】:2014-09-13 14:55:30
【问题描述】:

我在 TableView 中有一个文本框,当用户选择一个开关时,它会出现(变得可见)并弹出一个键盘,当用户关闭开关时,TextBox 消失并且键盘隐藏。在键盘出现和消失的时间里,我想将 tableView 稍微向上移动,然后回到原来的位置。这是代码

-(IBAction)actionSwitch:(id)sender
{
isSearchTerm = [switchSearchTerm isOn];
[self.tableView reloadData];
if(isSearchTerm == YES)
{
    [txtSearchTerm becomeFirstResponder];
    floatBottom = self.tableView.contentInset.bottom;

    self.tableView.contentInset=UIEdgeInsetsMake(0,0,200,0);
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
else
{
    [txtSearchTerm resignFirstResponder];
    self.tableView.contentInset=UIEdgeInsetsMake(0,0,floatBottom,0);
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForSelectedRow] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}

else 部分无法将 tableview 恢复到原来的位置,请帮忙。

【问题讨论】:

    标签: ios uitableview uiresponder


    【解决方案1】:

    如果您的 tableView 位于导航控制器或导航控制器的 rootViewController 中,并且您在 iOS7+ 上执行所有这些操作,UIViewController 的属性 automaticallyAdjustsScrollViewInsets 设置为 YES。您最好按照文档建议处理键盘出现

    // Call this method somewhere in your view controller setup code.
    - (void)registerForKeyboardNotifications
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                selector:@selector(keyboardWasShown:)
                name:UIKeyboardDidShowNotification object:nil];
    
       [[NSNotificationCenter defaultCenter] addObserver:self
                 selector:@selector(keyboardWillBeHidden:)
                 name:UIKeyboardWillHideNotification object:nil];
    
    }
    
    // Called when the UIKeyboardDidShowNotification is sent.
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    
        // If active text field is hidden by keyboard, scroll it so it's visible
        // Your app might not need or want this behavior.
        CGRect aRect = self.view.frame;
        aRect.size.height -= kbSize.height;
        if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
            [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
        }
    }
    
    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
        UIEdgeInsets contentInsets = UIEdgeInsetsZero;
        scrollView.contentInset = contentInsets;
        scrollView.scrollIndicatorInsets = contentInsets;
    }
    

    还有另一种方法可以做到这一点。两者都可以在“移动位于键盘下的内容”“iOS 文本编程指南”中找到

    【讨论】:

    • 我认为您的代码所做的实际上是相同的事情并且最终工作方式相同,就像我的一样,一旦向上移动的表格视图不会下降。
    • 通过在 ViewControllers 属性中禁用顶部栏属性下的扩展解决了这个问题。还是谢谢
    【解决方案2】:

    我通过选择 ViewController 在属性检查器中的视图控制器属性下将顶栏属性下的扩展设置为 NO 解决了这个问题。

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多