2016-08-17 30 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