作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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];
}
}
最佳答案
如果您的 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 - ContentInsets 不适用于 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824378/
我是一名优秀的程序员,十分优秀!