简体   繁体   中英

How to change the background color of navigation bar in a modal view?

I present a modal view when I click a cell in my custom tableview. How to change the background color of navigation bar in a modal view? For example. I want to change it to black color.The following code do not work in the prepare segue method.

[destinationNavController.navigationBar setBackgroundColor:[UIColor blackColor]];

or

 [[UINavigationBar appearance]setBackgroundColor:[UIColor blackColor]];

or

 [destinationNavController.navigationBar setTintColor:[UIColor blackColor]];

in the viewDidLoad method of the destination Controller view I write

[self.navigationController.navigationBar setBackgroundColor:[UIColor blackColor]];

or

 [[UINavigationBar appearance]setBackgroundColor:[UIColor blackColor]];

or

 [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];// only change the navigation item text color....

Any other ideas?

This the right way to present the modal controller and change the nav bar color, write the below code:-

[self presentModalViewController:customViewController animated:YES];

customViewController.topViewController.navigationController.navigationBar.tintColor = [UIColor blackColor];

If you are on ios 7 or later, you need to use the barTintColor property of UINavigationBar .

Like this:

self.navigationController.navigationBar.barTintColor = [UIColor blackColor];

I only tested this in iOS 11 so far and in my case our app is white but I want to present a fullscreen slide show with black color, but I still wanted a status bar, with white letters and the same black background as my main view

Step 1: make your view background color black. XCode in your .xib or storyboard the parents view's background color black (very important step)

Step 2: in your view controller that is in charge of this view, add this:

override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent }

This makes the color of the status text to be white. This also works on iPhone X.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM