search时状态栏变色

我使用的是一个UISearchController ,点击后,导航栏上升,状态栏变成白色。 我想状态栏保持绿色。

我的UISearchController实现:

 self.resultSearchController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.searchBar.sizeToFit() controller.searchBar.placeholder = "Pesquisa por linha ou cod..." controller.searchBar.tintColor = UIColor(netHex: 0xFFFFFF) controller.searchBar.barTintColor = UIColor(netHex: 0x2F7C30) controller.searchBar.clipsToBounds = true controller.searchBar.layer.borderWidth = 1; controller.searchBar.layer.borderColor = UIColor(netHex: 0x2F7C30).CGColor self.navigationController!.view.backgroundColor = UIColor(netHex: 0x2F7C30) self.tableView.tableHeaderView = controller.searchBar return controller })() 

在点击search栏之前 在点击搜索栏之前

点击search栏后 点击搜索栏后

丹尼尔·斯通的回应。 添加子视图到UISearchController的视图解决了我的问题。

 let statusBarView = UIView(frame: CGRectMake(0, 0, UIApplication.sharedApplication().statusBarFrame.size.width, UIApplication.sharedApplication().statusBarFrame.size.height)) statusBarView.backgroundColor = Colors.Primary.Regular searchController.view.addSubview(statusBarView) 

把你的需要的背景颜色放在你的状态栏后面的UIView

 let statusBarView = UIView(frame: UIApplication.sharedApplication().statusBarFrame) statusBarView.backgroundColor = UIColor.greenColor() // Replace with color you desire view.addSubview(statusBarView) 

尝试这个:

 self.navigationController?.navigationBar.isTranslucent = false 

对于目标C版本,您可以使用以下代码:

 UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].statusBarFrame.size.width, [UIApplication sharedApplication].statusBarFrame.size.height)]; statusBarView.backgroundColor = [UIColor searchBarBackgroundColor]; 

[resultSearchController.view addSubview:statusBarView];