如何将导航栏颜色应用到扩展中的状态栏?
如果在动作扩展中,我改变了导航栏的颜色,如下所示:
class ActionViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() UINavigationBar.appearance().barTintColor = UIColor.green
然后结果看起来像这样,状态栏是白色的:
但是,如果将同一行( UINavigationBar.appearance().barTintColor
)应用于应用程序(而不是扩展),则结果会不同,例如,如果将以下行添加到XCode主细部模板项目中:
class MasterViewController: UITableViewController { override func viewDidLoad() { UINavigationBar.appearance().barTintColor = UIColor.green
然后这是状态栏也是绿色的结果:
我怎样才能让状态栏在动作扩展中显示为绿色?
这是因为默认的ActionViewController
使用UINavigationBar
而没有UINavigationController。如果你检查ActionViewController
的故事板,你可以看到UINavigationBar
比在UINavigationController中使用的时间短20分。
statusBar后面的视图是rootView而不是NavigationBar。
解决scheme是:
1.改变你的viewController结构来包含一个UINavigationController。
2.在状态栏下方添加一个statusBarBackgroundview,并更改它的颜色:
- (void)viewDidLoad { [super viewDidLoad]; [UINavigationBar appearance].barTintColor = [UIColor greenColor]; UIView* statusBarBackgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)]; statusBarBackgroundView.backgroundColor = [UIColor greenColor]; [self.view insertSubview:statusBarBackgroundView atIndex:0]; }