2016-08-17 88 views
1

如果操作扩展中我改变导航栏的颜色如下:如何将导航栏颜色应用到扩展中的状态栏?

class ActionViewController: UIViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     UINavigationBar.appearance().barTintColor = UIColor.green 

那么结果是这样的,在白色的状态栏:

enter image description here

但如果同一行(UINavigationBar.appearance().barTintColor)应用于应用程序(与扩展相反),则结果会不同,例如,如果将以下行添加到XCode主细部模板项目中:

class MasterViewController: UITableViewController { 
    override func viewDidLoad() { 
     UINavigationBar.appearance().barTintColor = UIColor.green 

那么这是状态栏的结果还绿:

enter image description here

我怎样才能获得状态栏出现绿色行动延期?

+0

[试试这个(http://stackoverflow.com/a/34658477/1378447)​​让我们知道结果。 –

回答

1

这是因为默认ActionViewController使用UINavigationBar没有UINavigationController.If您检查ActionViewController故事板,你可以看到UINavigationBar是当它在一个UINavigationController的二手短于20分。

statusBar后面的视图是rootView而不是NavigationBar。

解决的办法是:

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]; 
} 
+0

谢谢,所以SO不会让我奖励赏金,我会在这样做的时候这样做。 – Gruntcakes