ios - ContentInsets 不适用于 UITableView

标签 ios uitableview uiresponder

我在 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 带回其原始位置,请帮助。

最佳答案

如果您的 tableView 位于导航 Controller 或导航 Controller 的 rootViewController 中,并且您在 iOS7+ 上使用 UIViewController 执行所有这些操作的属性(property) automaticallyAdjustsScrollViewInsets设置为是。你最好处理键盘出现的文件建议

// 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 文本编程指南”中找到

关于ios - ContentInsets 不适用于 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824378/

相关文章:

iphone - iOS 识别触摸但不拦截它

ios - UIControl Target Action 事件不会在响应者链中向上流动

iphone - 如何使 iPhone 应用程序中的 SQLite 数据库可用于桌面?

ios - 以编程方式将 UIButton 添加到 UITableView 但按钮不显示

ios - 像在 IOS 中一样更改 ionic 后退按钮文本

ios - 无法使用 JSON 数据设置 UITableView 部分标题

ios - 是否可以突出显示 tableView 单元格内的文本子字符串?

ios - 带有 inputAccessoryView 的 UIViewController 未被释放

ios - 使用自动布局增加 ScrollView 中的 View 高度

ios - 如何在 facebookSDK v4.0.1 上找到 facebookSDK.framework?