当搜索栏具有焦点时,UISearchController 不会尊重隐藏的状态栏

UISearchController won't honor hidden status bar when search bar has focus

我的应用程序通过 plist 条目 + appdelegate 中的这个隐藏了状态栏:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  [application setStatusBarHidden:YES];  

当我的视图加载时,状态栏是隐藏的。只要我点击并将焦点放在搜索栏上,状态栏就会出现。当搜索栏失去焦点时,状态栏又消失了。

如何在搜索栏获得焦点时不显示状态栏?

它发生在我身上!! 我在 info.plist 中有 "Status bar is initially hidden",还有函数(在我的例子中是 swift):

override func prefersStatusBarHidden() -> Bool {
    return true;
}

但是只要我点击搜索栏,状态栏就会显示为 ZJones 刚才所说的。

我在 info.plist 中添加了这个条目:

"View controller-based status bar appearance" as NO

容易但真的很难找到!!

来自 Apple 文档:

iOS 7 gives view controllers the ability to adjust the style of the status bar while the app is running. A good way to change the status bar style dynamically is to implement preferredStatusBarStyle and—within an animation block—update the status bar appearance and call setNeedsStatusBarAppearanceUpdate.

Note

If you prefer to opt out of having view controllers adjust the status bar style—and instead set the status bar style by using the UIApplicationstatusBarStyle method—add the UIViewControllerBasedStatusBarAppearance key to an app’s Info.plist file and give it the value NO.

希望对你有用!

当在父控制器上点击搜索栏时,如何显示 (hide/show) 状态栏的控制权将交给搜索控制器。

在 info.plist 中将 "View controller-based status bar appearance" 设置为 NO 在一定程度上起作用,防止在点击搜索时出现状态栏,尽管这也阻止了状态栏出现在其他视图控制器上;不受欢迎的行为。

相反,我认为子 class UISearchController 并覆盖那里的 prefersStatusBarHidden 方法更准确。 (我还在 info.plist 中将 "View controller-based status bar appearance" 设置为 YES)。

所以:

@implementation MySearchController

// Override so it does not show the status bar when active
- (BOOL)prefersStatusBarHidden {
     return YES;
}    
...
@end

并在呈现控制器中:

_searchController = [[MySearchController alloc] initWithSearchResultsController:nil];