Search code examples
iosstatusbar

Status bar color does not match navigation bar


I have a view controller I am presenting modally. I want the status bar color to match the navigation bar color.

I have set UIViewControllerBasedStatusBarAppearance to YES because I don't want this change across the entire application.

I am setting self.navigationController.navigationBar.barTintColor but this is only changing the navigation bar color. The status bar remains a lighter color.

I have tried various combinations of setNeedsStatusBarAppearanceUpdate and preferredStatusBarStyle but none have any effect.

enter image description here

View controller is launched like so:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:searchController];

navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

[self presentViewController:navigationController animated:YES completion:nil];

Solution

  • My steps are below, check where you go wrong:

    1. Create 2 view controller like below, you can let vc1 embed in a navigation controller:

    enter image description here

    1. In the vc1, you drag a action of the button:

    enter image description here

    Code is below:

    - (IBAction)clickAction:(UIButton *)sender {
    
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ViewController2 *searchController = [sb instantiateViewControllerWithIdentifier:@"ViewController2"];
    
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:searchController];
    
        navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
    
        [self presentViewController:navigationController animated:YES completion:nil];
    }
    
    1. Then in the vc2, you set the title:

    enter image description here