UISearchController在旋转时不重新显示导航栏

时间:2015-02-17 00:57:02

标签: ios uisearchbar uisearchcontroller

我已经实现了UISearchController,除了...

之外它工作得很好

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

enter image description here

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

enter image description here

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

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;

5 个答案:

答案 0 :(得分:15)

当状态栏重新出现时,UISearchController似乎忘记重置searchBar的框架。我认为这可能是UISearchController中的一个错误;好像radar中列出了一些。看起来searchBar的超级视图(它是UISearchController的内部)最终会出现错误的高度。这很麻烦,因为解决方案因此涉及到进入searchController的视图层次结构,Apple可以更改...您可能想要添加对iOS版本的检查,以便它仅针对指定版本运行。

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

答案 1 :(得分:10)

您可能需要在视图控制器中实施UIBarPositioningDelegate

self.venueSearchController.searchBar.delegate = self;

searchBar正在寻找其代表的回复。

@protocol UISearchBarDelegate <UIBarPositioningDelegate> 

将以下内容添加到self(我认为它是一个ViewController)

#pragma mark - <UIBarPositioningDelegate>

// Make sure NavigationBar is properly top-aligned to Status bar
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    if (bar == self.venueSearchController.searchBar) {
        return UIBarPositionTopAttached;
    }
    else { // Handle other cases
        return UIBarPositionAny;
    }
}

答案 2 :(得分:1)

我认为这可能是UITableViewController布局处理导航栏的方式的问题,而不是在你旋转的情况下没有导航栏。

我认为您可以通过将UITableViewController替换为放置UITableView的UIViewController来解决此问题。然后将表视图的顶部约束设置为顶部布局指南。将边约束设置为0的左,右,下。

这应确保表始终保持在状态栏下方,并且当导航栏返回时仍会正确移动。

请参阅此讨论:iOS 7: UITableView shows under status bar

答案 3 :(得分:0)

UISearchControllernavigationItem一起使用时遇到了类似的问题。外观不同,但是由相同的问题引起:UISearchController不会在状态栏再次出现时更新searchBar框架。

但是,您无需手动调整searchBar的视图层次结构框架,而只需通过在hidesNavigationBarDuringPresentation或{{1 }}:

因此修复程序将如下所示:

traitCollectionDidChange

这是原始问题的样子。

Search in landscape without status bar

然后旋转并得到:

Search in portrait after status bar reappearing

答案 4 :(得分:-6)

我通过在设置中关闭“隐藏状态栏”暂时“修复”了这个。

enter image description here