Scrungepipes
Scrungepipes

Reputation: 37581

How to apply the navigation bar color to the status bar in an extension?

If within an action extension I change the color of the navigation bar as follows:

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

Then the result looks like this, with the status bar in white:

enter image description here

But if the same line (UINavigationBar.appearance().barTintColor) is applied to an application (as opposed to an extension) then the result is different, for example if the following line is added to the XCode master-detail template project:

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

Then this is the result with the status bar also green:

enter image description here

How can I get the status bar to appear green in the action extension?

Upvotes: 2

Views: 2057

Answers (1)

wj2061
wj2061

Reputation: 6885

It's because the default ActionViewController use a UINavigationBar without UINavigationController.If you check the storyboard of ActionViewController,you can see the UINavigationBar is 20 points shorter than when it's used in a UINavigationController.

The view behind the statusBar is the rootView not the NavigationBar.

The solution is :

1.change your viewController structure to include a UINavigationController.

2.add a statusBarBackgroundview below statusbar,and change it's color :

- (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];
}

Upvotes: 5

Related Questions