简体   繁体   English

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

[英]UITableview doesn't scroll up when keyboard is shown

I'm having a startview like this: 我有这样一个startview:

在此输入图像描述

Where The first half of the screen is a Google map and the second half is a UITableview with in the header cell a UISearchbar. 屏幕的前半部分是Google地图,后半部分是UITableview,在标题单元格中有一个UISearchbar。

When I click on the searchbar this happens on big screens but on small screens my uisearchbar is covered with the keyboard. 当我点击搜索栏时,这会发生在大屏幕上,但在小屏幕上,我的uisearchbar被键盘覆盖。

在此输入图像描述

As you can see , when the keyboard shows up the searchbar moves a little bit to the bottom. 如您所见,当键盘显示时,搜索栏会向下移动一点点。 I don't want that. 我不希望这样。 It should push everything up so the Tableview is visible. 它应该推动一切,以便Tableview可见。

I followed a tutorial about contentInset. 我按照了关于contentInset的教程。 But it doesn't do anything. 但它没有做任何事情。

- (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;
}

It comes in the if test where Scrolled is printed. 它出现在打印滚动的if测试中。 And still nothing happens. 但仍然没有任何反应。

Where _searchBar is my UISearchbar variable. _searchBar是我的UISearchbar变量。

Anyone an idea? 有人有想法吗?

EDIT: 编辑:

Solved the problem except the UISearchbar jump crapy down when clicking on it. 解决了问题,除了UISearchbar跳转crapy时点击它。 This creates a gap like you can see on the screenshot: 这会产生一个像你在屏幕截图中看到的差距:

在此输入图像描述

See the example below. 请参阅下面的示例。

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



}

If your searchbar, tableview and mapview is all place in a view, 如果您的搜索栏,tableview和mapview都放在视图中,

and you want to push the tableview and searchbar visible when keyboardWillshow, 并且你想在keyboardWillshow时推送tableview和搜索栏,

you should update frame instead of contentInset . 你应该更新frame而不是contentInset

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM