I'm trying to get this blur-translucent navigation bar effect (The first navigation bar in the image) in my Swift iOS application:
But I can't get this done. I've tried several methods, some of them are below:
setBackgroundImage, shadowImage and translucent attrs in AppDelegate (Getting the NavController from the RootViewController attr)
Those lines in AppDelegate main method:
UINavigationBar.appearance().translucent = true;
UINavigationBar.appearance().barTintColor = UIColor(white: 1, alpha: 0.4)
Changing attributes from UIBuilder
Setting self.automaticallyAdjustsScrollViewInsets = true
and self.edgesForExtendedLayout = UIRectEdge.None
in viewControllers
But still can't get this to work. I'm running the app on XCode simulators, using iOS 8.1 and 9.0
What I am doing wrong?
A Boolean value that indicates whether the navigation bar is translucent.
Apply a UIVisualEffectView to an existing view to apply a blur or vibrancy effect to the exiting view. After you add the UIVisualEffectView to the view hierarchy, add any subviews to the contentView of the UIVisualEffectView . Do not add subviews directly to the UIVisualEffectView itself.
The second navigation bar is already translucent and blurred—it just isn't quite as translucent as the first navigation bar. If you open Digital Color Meter and mouse over your screenshot, you can see that the background of the second navigation bar is darker in the area covering the image.
The reason why the first navigation bar is more translucent than the second navigation bar was explained in this question: the screenshot of the first bar was taken from a device running iOS 7.0.2, whereas the screenshot of the second bar was taken from a device running iOS 7.0.4. Apple changed the behaviour of UINavigationBar between those versions of iOS, and again with iOS 7.1.
Apple explains the current translucency mechanism as follows:
A translucent bar mixes its
barTintColor
with gray before combining it with a system-defined alpha value to produce the final background color that is used to composite the bar with the content it overlies.
The only description of the blur effect appears to be the following:
Navigation bars are translucent by default in iOS 7. Additionally, there is a system blur applied to all navigation bars.
You can change the transparency of the bar by using UIImage.imageWithColor:
to make a background image for the bar from a UIColor with an appropriate alpha value, but doing so will remove the blur effect. There is no documented way to alter the "system-defined alpha value" to increase the translucency of the bar without removing the "system blur".
If you want to imitate the old translucency and blurring effects on iOS 8.1 and 9.0, you'll need to make your own subclass of UINavigationBar and insert one or more subviews such as a UIView with a background color that uses an appropriate alpha value (for transparency) and/or a UIVisualEffectView (for the blur effect).
You might be able to find a combined view in one of the answers to this question.
Related: Blurring effect disappeared on iOS 7.1
Add bellow code in didFinishLaunchingWithOptions:
For Objective C
[[UINavigationBar appearance] setTranslucent:NO];
For Swift 3+/iOS 10+
UINavigationBar.appearance().isTranslucent = false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With