显示键盘时,UITableview不会向上滚动

时间:2014-08-19 07:28:30

标签: ios objective-c uitableview

我有这样的起始视图:

enter image description here

屏幕的前半部分是Google地图,后半部分是UITableview,在标题单元格中有一个UISearchbar。

当我点击搜索栏时,这会在大屏幕上发生,但在小屏幕上,我的uisearchbar会被键盘覆盖。

enter image description here

正如您所看到的,当键盘显示时,搜索栏会向下移动一点点。我不想要那个。它应该推动一切,以便可以看到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时点击它。这会产生一个像你在屏幕截图中看到的差距:

enter image description here

2 个答案:

答案 0 :(得分:2)

请参阅下面的示例。

- (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)];



}

答案 1 :(得分:1)

如果您的搜索栏,tableview和mapview都放在视图中,

并且你想在keyboardWillshow,

时推送tableview和搜索栏

您应该更新frame而不是contentInset