If keyboard is not dismissed, segueing when using a search bar changes destination view controller layout

时间秒杀一切 提交于 2019-12-13 06:13:13

问题


I have a UITableView where I display a list of chat rooms. I'm also using a search bar to find an specific chat room. When a user taps into a cell, it segues to the chat room conversation, displaying the messages and a text box to send messages.

However, if I seach a chat room using the search bar, when I tap in any cell the segue is done but the layout of the chat room is wrong. The conversation view (custom UITableView) fills the whole screen and the text box (located at the bottom, just in top of a TabBar) doesn't appear.

Trying to solve this issue, I've discovered that I was able to segue correctly when searching if I dismissed the keyboard before tapping into any cell, so it seems that this is only happening when the keyboard is present.

I think I can solve this issue programatically dismissing the keyboard before segueing, but I'm also afraid that maybe I'm doing something wrong with the UISearchBaror the UITableView.

My target project is iOS 5.1 and I've reproduced this issue in an iPad 3 running 5.1, iPad 2 running 6.01 and iPhone simulators.

EDIT

This is the code I'm using to segue

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchController.searchResultsTableView == tableView) {
        self.selectedItem = [self.filteredItems objectAtIndex:[indexPath row]];
    } else {
        self.selectedItem = [self.items objectAtIndex:[indexPath row]];
    }
    [self performSegueWithIdentifier:@"segueChatRoom" sender:self];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self.view endEditing:YES]; // This line solves the issue
    UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"Custom title" style: UIBarButtonItemStyleBordered target: nil action: nil];
    [[self navigationItem] setBackBarButtonItem: newBackButton];
    if ([segue.identifier isEqualToString:@"segueChatRoom"]) {
        [segue.destinationViewController setItem:self.selectedItem];
    }
}

回答1:


As you already pointed out you need to resign the first responder (lower the keyboard). Your [self.view endEditing:YES]; is essentially doing this.



来源:https://stackoverflow.com/questions/13837689/if-keyboard-is-not-dismissed-segueing-when-using-a-search-bar-changes-destinati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!