Layout of UISearchBar in UITableView header messed up after rotation

丶灬走出姿态 提交于 2019-12-01 08:44:45

After noting that an inactive searchBar doesn't seem to have this problem, I have had success with saving the search bar state when the view disappears and restoring it before the view reappears.

The sample code works fine with the following changes. Save the state:

- (void)viewDidDisappear:(BOOL)animated
{
    if (self.searchController.active && self.searchController.searchBar.text.length > 0) {
        self.savedSearch = self.searchController.searchBar.text;
        [self disableSearch];
    } else if (self.searchController.active) {
        // empty search field - this won't get restored
        [self disableSearch];
    }
    [super viewDidDisappear:animated];
}

- (void)disableSearch
{
    if (self.searchController.isActive) {
        self.searchController.searchBar.text = @"";
        self.searchController.active = NO;
    }
}

And restore:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (self.savedSearch) {
        NSLog(@"RESTORED SEARCH");
        self.searchController.searchBar.text = self.savedSearch;
        self.searchController.searchBar.showsCancelButton = YES;
        self.searchController.active = YES;
        self.savedSearch = nil;
    }
}

This seems to work fine whether or not self.searchController.hidesNavigationBarDuringPresentation is set (although if true there is some animation on returning to the view).

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