How to apply color of navigation bar to status bar in extension?

If in an extension of an action 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 will look like this: status bar in white:

enter image description here

But if the same line ( UINavigationBar.appearance().barTintColor ) is applied to the application (as opposed to the extension), then the result is different, for example, if the following line is added to the Xcode project of the main part template:

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

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

enter image description here

How can I make the status bar appear green in an extension of an action?

+1
ios
source share
1 answer

This is because, by default, the ActionViewController uses the UINavigationBar without the UINavigationController. If you check the ActionViewController storyboard, you can see that the UINavigationBar is 20 points shorter than when using the UINavigationController.

The view behind the status is the rootView, not the NavigationBar.

Decision:

1. modify the structure of the viewController to include the UINavigationController.

2.add statusBarBackgroundview under the status bar and change the 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]; } 
+1
source share

All Articles