ios – UISearchController不重新显示旋转导航栏

前端之家收集整理的这篇文章主要介绍了ios – UISearchController不重新显示旋转导航栏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实施了UISearchController,除了…

当我点击搜索栏时,导航栏将按照预期的方式消失.当我将手机旋转到横向视图时,我得到这个观点是有道理的.

但是,当我将手机旋转回到纵向视图(仍在搜索类型区域中选择)时,我得到以下视图.

您可以看到导航栏从不重新出现.我觉得我正在实施一个基本的搜索控制器.可能是什么原因造成的?

self.venueSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.venueSearchController.searchResultsUpdater = self;
self.venueSearchController.searchBar.delegate = self;
self.venueSearchController.dimsBackgroundDuringPresentation = NO;
self.venueSearchController.hidesNavigationBarDuringPresentation = YES;
self.venueSearchController.searchBar.frame = CGRectMake(self.venueSearchController.searchBar.frame.origin.x,self.venueSearchController.searchBar.frame.origin.y,self.venueSearchController.searchBar.frame.size.width,44.0);

self.definesPresentationContext = YES;

self.navigationController.navigationBar.translucent = YES;
self.venueSearchController.searchBar.translucent = YES;

self.tableView.tableHeaderView = self.venueSearchController.searchBar;

解决方法

看起来UISearchController忘记在状态栏重新出现时重新设置searchBar的框架.我认为这可能是UISearchController中的一个错误;似乎有一些列在 radar.似乎searchBar的超级视图(这是内部的UISearchController)最终以错误的高度.这是令人烦恼的,因为解决方案因此涉及到SearchController的视图层次结构,哪个苹果可以改变…你可能想添加一个iOS版本的检查,所以它只运行指定的版本.

如果将下面的代码添加到视图控制器,则在trait集合更改时将调用代码.它检查a)搜索控制器是否处于活动状态,b)statusBar不被隐藏,并且c)searchBar origin-y为0,如果是,则将superview的高度增加到statusBar的高度,searchBar下来.

override func traitCollectionDidChange(prevIoUsTraitCollection: UITraitCollection?) {
    let app = UIApplication.sharedApplication()
    if searchController!.active && !app.statusBarHidden && searchController?.searchBar.frame.origin.y == 0 {
        if let container = self.searchController?.searchBar.superview {
            container.frame = CGRectMake(container.frame.origin.x,container.frame.origin.y,container.frame.size.width,container.frame.size.height + app.statusBarFrame.height)
        }
    }
}

目标C

- (void) traitCollectionDidChange: (UITraitCollection *) prevIoUsTraitCollection {

    [super traitCollectionDidChange: prevIoUsTraitCollection];

    if(self.venueSearchController.active && ![UIApplication sharedApplication].statusBarHidden && self.venueSearchController.searchBar.frame.origin.y == 0)
    {
        UIView *container = self.venueSearchController.searchBar.superview;

        container.frame = CGRectMake(container.frame.origin.x,container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height);
    }
}

猜你在找的iOS相关文章