UISearchController在旋转时不重新显示导航栏 [英] UISearchController Not Redisplaying Navigation Bar on Rotate

查看:67
本文介绍了UISearchController在旋转时不重新显示导航栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了UISearchController,除了...之外它工作得很好。

I have implemented the UISearchController and it is working great except...

当我点击搜索栏时,导航栏会按预期很好地消失。当我将手机旋转到横向视图时,我得到了这个有意义的视图。

When I click on the Search Bar the Navigation Bar disappears nicely as expected. When I rotate the phone to landscape view I get this view which makes sense.

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

However, when I rotate the phone back to portrait view (still selected in search type area) I get this following view.

您可以看到导航栏永远不会再出现。我觉得我正在实现一个基本的搜索控制器。 可能造成这种情况的原因是什么?

You can see that the Navigation Bar never reappears. I feel I'm implementing a basic search controller. What could possibly be causing this?

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 中的一个错误;似乎有一些列在雷达中。似乎searchBar的superview(它是UISearchController的内部)最终导致错误的高度。这是令人烦恼的,因为解决方案因此涉及到达searchController的视图层次结构,Apple可以更改...您可能想要添加iOS版本的检查,以便它仅针对指定版本运行。

It looks like the UISearchController forgets to reset the frame of the searchBar when the status bar reappears. I think this is probably a bug in UISearchController; there seem to be a few listed in radar. It seems the searchBar's superview (which is internal to the UISearchController) ends up with the wrong height. This is vexing since the solution therefore involves reaching into the searchController's view hierarchy, which Apple could change... you might want to add a check of the iOS version so it only runs for specified versions.

如果将以下代码添加到视图控制器,则在特征集合更改时将调用它。它检查以查看a)搜索控制器是否处于活动状态,b)statusBar未被隐藏,以及c)searchBar origin-y是0,如果是,它会将superview的高度增加statusBar的高度, searchBar down。

If you add the code below to your view controller, it will be called when the trait collection changes. It checks to see a) the search controller is active, b) the statusBar is not hidden, and c) the searchBar origin-y is 0, and if so it increases the height of the superview by the height of the statusBar, which moves the searchBar down.

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.origin.y, container.frame.size.width, container.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height);
    }
}

这篇关于UISearchController在旋转时不重新显示导航栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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