Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safari View Controller uses wrong status bar color

My app uses a dark navigation bar color. Therefore I set the status bar color to white (so it has a nice contrast).

red navigation bar with white status bar

I did this by setting the barStyle to black (to make the status bar white) and also setting the barTint to my dark red color. Works perfectly.

I present a SafariViewController like this:

func openWebsite(urlString: String) {
    if let url = NSURL(string: urlString) {
        let svc = SFSafariViewController(URL: url)
        svc.delegate = self
        self.presentViewController(svc, animated: true, completion: nil)
    }
}

However the status bar of the presented SafariViewController still is white. This is a problem because the SVC navigation bar has the default white transparent iOS default style. So the status bar is basically invisible.

safari view controller with white status bar color

How can I fix that?

like image 666
funkenstrahlen Avatar asked Feb 02 '16 08:02

funkenstrahlen


People also ask

Why is navigation bar black?

One of the issues that can occur is coloring the nav bar when your keyboard is open for typing. To fix this, head into General Settings –> Color of Navigation Bar when Keyboard is opened. Set this color to black as well. This should fix any issues you may have when the keyboard is open.

How do I change the color of my status bar in Swiftui?

You can change the status bar's color and material by inserting a small view right behind it. Normally, all views are positioned below the status bar, but you can use edgesIgnoringSafeArea(. top) to push it past the safe area insets.

How to change the status bar style of the viewcontroller?

The style of the status bar can be changed to a status bar with white content. Go to the ViewController.swift file and add the following lines of code. override var preferredStatusBarStyle: UIStatusBarStyle { return.lightContent } The preferredStatusBarStyle property is set to lightContent.

Why is the status bar dark in my iOS project?

Build and Run the project, The content of the status bar is dark again, which is the default. The reason for this is, iOS asked for the style of the status bar of the navigation controller instead of the contained view controller.

How do I change the color of the status bar?

Select the View and in the Attributes Inspector change the Background Color to Light Gray. Build and Run the Project. The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content.

How to change the default status bar style in Swift?

The default style of the status bar is dark content. The style of the status bar can be changed to a status bar with white content. Go to the ViewController.swift file and add the following lines of code. override var preferredStatusBarStyle: UIStatusBarStyle { return.lightContent }


1 Answers

You can achieve that by wrapping SFSafariViewController with subclassed UINavigationController.

BlackStatusBarNavigationController.h

@interface BlackStatusBarNavigationController : UINavigationController
@end

BlackStatusBarNavigationController.h

@interface BlackStatusBarNavigationController ()

@end

@implementation BlackStatusBarNavigationController

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

@end

Use like this:

UINavigationController *navigationController = [[BlackStatusBarNavigationController alloc] initWithRootViewController:viewController];
navigationController.navigationBarHidden = YES;

[self presentViewController:navigationController animated:YES completion:nil];
like image 52
konradholub Avatar answered Oct 23 '22 08:10

konradholub