![](/img/trans.png)
[英]Why does the UITableVIew scroll down but not up when keyboard is shown?
[英]UITableview doesn't scroll up when keyboard is shown
我有这样一个startview:
屏幕的前半部分是Google地图,后半部分是UITableview,在标题单元格中有一个UISearchbar。
当我点击搜索栏时,这会发生在大屏幕上,但在小屏幕上,我的uisearchbar被键盘覆盖。
如您所见,当键盘显示时,搜索栏会向下移动一点点。 我不希望这样。 它应该推动一切,以便Tableview可见。
我按照了关于contentInset的教程。 但它没有做任何事情。
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
NSLog(@"KeyboardWillshow");
// Step 1: Get the size of the keyboard.
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
// Step 3: Scroll the target text field into view.
CGRect aRect = self.view.frame;
aRect.size.height -= keyboardSize.height;
if (!CGRectContainsPoint(aRect, _searchBar.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, _searchBar.frame.origin.y - (keyboardSize.height-15));
[self.tableView setContentOffset:scrollPoint animated:YES];
NSLog(@"Scrolled");
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}
它出现在打印滚动的if测试中。 但仍然没有任何反应。
_searchBar是我的UISearchbar变量。
有人有想法吗?
编辑:
解决了问题,除了UISearchbar跳转crapy时点击它。 这会产生一个像你在屏幕截图中看到的差距:
请参阅下面的示例。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
float height =150;
float yPoint = aRect.size.height-height;
[self.tblSearch setFrame:CGRectMake(self.tblSearch.frame.origin.x
, yPoint, self.tblSearch.frame.size.width, height)];
}
如果您的搜索栏,tableview和mapview都放在视图中,
并且你想在keyboardWillshow时推送tableview和搜索栏,
你应该更新frame
而不是contentInset
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.