旋转后 UITableView 标头中 UISearchBar 的布局混乱 [英] Layout of UISearchBar in UITableView header messed up after rotation

查看:12
本文介绍了旋转后 UITableView 标头中 UISearchBar 的布局混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有搜索栏的 UITableView 以编程方式插入到表的 headerView 中:

I have a UITableView with a search bar inserted programatically into the table's headerView:

override func viewDidLoad() {

    super.viewDidLoad()
    resultSearchController = UISearchController(searchResultsController: nil)
    resultSearchController.searchResultsUpdater = self
    resultSearchController.dimsBackgroundDuringPresentation = false
    resultSearchController.searchBar.delegate = self
    resultSearchController.searchBar.placeholder = "Search Contacts"
    resultSearchController.searchBar.scopeButtonTitles = ["All", "Title1", "Title2", "Title3"]

    tableView.tableHeaderView = resultSearchController.searchBar
    resultSearchController.searchBar.sizeToFit()

    definesPresentationContext = true

    tableView.tableFooterView = UIView(frame: CGRectZero)

}`

该表设置为基于标准 UINavigationController 的向下钻取 - 在详细视图控制器中选择行推送.

The table is set up as a standard UINavigationController-based drill down - selecting a row pushes in a detail view controller.

一切都在纵向或横向上完美运行,但如果在显示时旋转了详细视图,则会在弹出详细视图后崩溃.确切的顺序是:

Everything works perfectly in portrait or landscape, but breaks down after the detail view is popped if is has been rotated while displayed. The exact sequence is:

  • 纵向打开表格,使用搜索栏过滤结果,然后选择其中一个结果推送到详细视图中
  • 在详细视图中,旋转为横向
  • 仍在细节视图中时,旋转回纵向

当表格视图重新出现时,搜索栏的所有单个元素都已经折叠"在彼此的顶部,整个东西都覆盖在状态栏上:

When the table view reappears, all the individual elements of the search bar have been "collapsed" on top of each other, and the whole thing overlays the status bar:

我尝试了各种方法在 viewDidAppear 中显式设置搜索栏的框架,以及从标题视图中删除和替换它 - 但问题仍然存在.

I've tried various ways of explicitly setting the frame of the search bar in viewDidAppear, as well as removing and replacing it from the header view - but the problem persists.

我还注意到来自 https://developer.apple.com/library/prerelease/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html:

有人想出解决方法吗?

编辑添加:

进一步挖掘,问题似乎是导航栏在旋转后消失了 - 这是之前的:

On further digging, it looks like the problem is that the navigation bar disappears after the rotation - here's before:

之后:

那么,有什么想法吗?

推荐答案

在注意到一个非活动的 searchBar 似乎没有这个问题后,我已经成功保存了搜索栏状态时视图消失并在视图重新出现之前恢复它.

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

然后恢复:

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

无论是否设置了 self.searchController.hidesNavigationBarDuringPresentation,这似乎都可以正常工作(尽管如果 true 在返回视图时会有一些动画).

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).

这篇关于旋转后 UITableView 标头中 UISearchBar 的布局混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆