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

ahe*_*ick 12 uisearchbar 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;
Run Code Online (Sandbox Code Playgroud)

pba*_*sdf 15

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

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

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)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

目标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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*ani 10

您可能需要UIBarPositioningDelegate在视图Controller中实现.

self.venueSearchController.searchBar.delegate = self;
Run Code Online (Sandbox Code Playgroud)

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

@protocol UISearchBarDelegate <UIBarPositioningDelegate> 
Run Code Online (Sandbox Code Playgroud)

添加以下内容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;
    }
}
Run Code Online (Sandbox Code Playgroud)