Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 -- Apply blur to navigation bar AND status bar

I am attempting to add a blur effect to a navigation bar and a status bar. My problem is that the blur goes great onto the navigation bar, but the status bar does not get blurred.

My question is: how can I extend the bounds to encompass the status bar?

I'm using the following method to create the blur effect:

- (void) addBlurEffect {

CGRect bounds = self.navigationController.navigationBar.bounds;
UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
visualEffectView.frame = bounds;
visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.navigationController.navigationBar addSubview:visualEffectView];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar sendSubviewToBack:visualEffectView];

}

In my plist, I have View controller-based status bar appearance YES

In viewDidLoad I call a method:

- (void)configureView {

    // style controls

    self.addAirportButton.tintColor = [UIColor whiteColor];

    // style background image

    UIImageView *sidebarBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sidebarBackground"]];
    self.tableView.backgroundView = sidebarBackground;

    // style navigation bar

    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;

    // this makes navigation bar transparent

    [self.navigationController.navigationBar setBackgroundImage:[UIImage new]
                                                  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;

    // style toolbar

    self.navigationController.toolbar.translucent = YES;
    self.dismissAdsButton.tintColor = [UIColor whiteColor];

Nothing else important gets done in viewDidLoad. When I build this, here is what the view looks like -- it's a tableViewController embedded in a NavigationController, and I'm using the excellent SWRevealViewController as well.

See how the status bar is not blurred:

The blur is only in the navigation bar part - the status bar is not blurred

Any help would be really appreciated!

UPDATE:

See answer below. Here is a screenshot of the implemented solution:

screenshot with solution applied

like image 463
Chris Holloway Avatar asked Feb 04 '15 23:02

Chris Holloway


People also ask

What do the colors in the status bar mean on iPhone?

On iPhone 8 or earlier, the color goes all the way across the status bar. Here's what each color means: Your iPhone is using Screen Mirroring , or an app is actively using your location. Your iPhone is either recording sound or your screen.

How to change the status bar style in iOS 7?

In iOS 7, you can control the style of the status bar from an individual view controller and change it while the app runs. If you prefer to opt out of this behavior and set the status bar style by using the UIApplication statusBarStyle method, add the UIViewControllerBasedStatusBarAppearance key to an app’s Info.plist file and give it the value NO

What is a navigation bar in an app?

A navigation bar appears at the top of an app screen, enabling navigation through a hierarchy of content. A navigation bar also provides a natural place to display a screen’s title — helping people orient themselves in your app or game — and it can include controls that affect the screen’s content.

How to make status and navigation bar transparent in Xcode?

Step 1 − Open Xcode → New Project → Single View Application → Let’s name it “TransparentViews” Step 2 − Embed the View Controller in Navigation Controller. Add Image View and shown and add image. Step 3 − Run the application without adding any piece of code for making status and navigation bar transparent.


1 Answers

I have been trying to accomplish similar effect, after tweaking with the UINavigationBar's API and could not achieve desired result, I found a workaround:

  1. create a UIView the same size as your NavigationBar + StatusBar. That is, it would have a frame of (0, 0, w, 64), where w is the width of your screen. (I did this through Storyboard and used autolayout to set the width constraint to be equal to that of its superview)

  2. set UIView's backgroundColor to clear color.

  3. make the navigationBar completely transparent with the following code: navBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default) navBar.shadowImage = UIImage() navBar.translucent = true

  4. now apply the blur effect to that View, this creates the illusion that the blur effect was from both the Navigation Bar and the Status Bar.

See this picture for achieved effect (sorry I can't post image yet due to reputation restriction).

Hope this helps.

UPDATE 2015-05-28:

This is how I achieved the aforementioned method in StoryBoard:

  1. Create the Navigation Bar Background View in the topmost hierarchy as a direct child of the main view. Note that everything else I have wrapped them into a single Content View. Content View is colored red and the Navigation Bar Background View is colored semi-transparent white.

enter image description here

  1. add following 4 constraints on the Navigation Bar Background View like such: 0 for left, right, and top constraint, and 64 for height constraint.

like image 70
saulgoodman Avatar answered Oct 07 '22 06:10

saulgoodman