我知道如何通过以下方式更改导航栏(和状态栏)的颜色:
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
但是当我隐藏导航栏时,状态栏颜色会恢复到透明的颜色。
如何在隐藏导航栏时仍保持状态栏颜色与barTintColor相同?
我知道如何通过以下方式更改导航栏(和状态栏)的颜色:
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
但是当我隐藏导航栏时,状态栏颜色会恢复到透明的颜色。
如何在隐藏导航栏时仍保持状态栏颜色与barTintColor相同?
let statusBarBGView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
statusBarBGView.backgroundColor = .whiteColor() //color of your choice
view.addSubview(statusBarBGView)
我的问题是我把navigationBar设置为隐藏,这导致statusBar的背景变得透明。这样一来,当tableView被滚动时,它的单元格会显示在statusBar的后面。
UIView
,然后更改颜色即可。 //The statusBarFrame returns the frame in screen coordinates. I believe the correct way to get what this corresponds to in view coordinates is to do the following:
- (CGRect)statusBarFrameViewRect:(UIView*)view
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect statusBarWindowRect = [view.window convertRect:statusBarFrame fromWindow: nil];
CGRect statusBarViewRect = [view convertRect:statusBarWindowRect fromView: nil];
return statusBarViewRect;
}
//source: https://dev59.com/5m865IYBdhLWcg3wTclY
然后使用以下代码在viewDidload方法中创建你的视图或者在隐藏导航栏时创建:
UIView *statusBarUnderLay = [[UIView alloc] initWithFrame:[self statusBarFrameViewRect:self.view]];
[statusBarUnderLay setBackgroundColor:[UIColor yellowColor]];
[self.view addSubview:statusBarUnderLay];
然后,就完成了。
在状态栏下添加一个 UIView
,并将其 backgroundColor
属性设置为导航栏的 barTintColor
我的解决方案很简单,就是设置视图控制器视图的背景颜色。例如:
[self.view setBackgroundColor:[UIColor blackColor]];
当操作系统选择不显示状态栏时,需要处理添加视图的情况。
这解决了我的问题:
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
self.navigationController?.navigationBarHidden = true
之后设置 self.tableView.contentInset = UIEdgeInsetsZero
。
UIView
放在其后面与将一个UINavigationBar
放在其后面没有任何区别。 - nhgrif