I use this code to blur navigation bar:
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
But my back button doesn't work and other button doesn't appear. How to fix it?
Just you should disable "isUserInteractionEnabled" to your visualEffectView
add the following line:
visualEffectView.layer.zPosition = -1;
visualEffectView.isUserInteractionEnabled = false
Related
How can I make this part of the app (the Navigation Bar) translucent? I don't want it to be fully transparent, I want it to show the website content's colours and be blurred.
Link with screenshot: iPhone 12 simulator with Navigation Bar Opaque
I have looked at tens of possibilities and none worked:
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController.navigationBar.translucent = YES;
I also tried:
let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (self.navigationController?.navigationBar.bounds)!
self.navigationController?.navigationBar.addSubview(blurEffectView)darkUIBlurEffect.Style
Also this:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default) //UIImage.init(named: "transparent.png")
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
And this:
self.navigationController?.navigationBar.isTranslucent = true
I also tried using the Main.storyboard's Attributes Inspector and ticked Translucent.
Attributes Inspector tab on Navigation Bar in Main.storyboard
No method worked. What should I do?
Try this:
extension UIViewController {
func blurNav(withAlpha alpha:CGFloat = 1.0 ) {
let statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let rect = navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight)) ?? .zero
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.frame = rect
blurView.isUserInteractionEnabled = false
blurView.alpha = alpha
blurView.layer.zPosition = -1
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.addSubview(blurView)
navigationController?.navigationBar.sendSubviewToBack(blurView)
}
}
Usage:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
blurNav(withAlpha: 0.8)
}
I am trying to blur the status bar depending on the background like it works when we pull down the status bar. like it is see through
let statusBarFrame = UIApplication.shared.statusBarFrame
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = statusBarFrame
view.addSubview(visualEffectView)
I have tried all styles but it does change to translucent
even I tried from storyboard effect shows in the storyboard but not in simulator even my Reduce Transparency is also turned on
I want to achive this in the stausbar
but it only gives a white bar
You can get your statusBar view by following code, then try add to visual effect like here
let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor(red: 213 / 255.0, green: 0 / 255.0, blue: 0 / 255.0, alpha: 0.7)
OR
extension UINavigationBar {
func installBlurEffect() {
isTranslucent = true
setBackgroundImage(UIImage(), for: .default)
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
var blurFrame = bounds
blurFrame.size.height += statusBarHeight
blurFrame.origin.y -= statusBarHeight
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.isUserInteractionEnabled = false
blurView.frame = blurFrame
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(blurView)
blurView.layer.zPosition = -1
}
}
Usage
navigationController?.navigationBar.installBlurEffect()
How to make the Navigation Bar and Status Bar blurred (UIBlurEffect)? When I'm by clicking on the image to scroll down (Scroll View) to other pictures, this picture (in this case with a white machine) is simply lost under the Navigation Bar, and it is necessary that this figure would be visible under the Navigation Bar with effect UIBlurEffect.
NO UIBlurEffect
I have tried so, but did not work:
func addBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds!
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
visualEffectView.isUserInteractionEnabled = false }
Firstly, when scrolling the picture just disappears under the Navigation Bar.
Second, Status Bar remains gray.
Bad UIBlurEffect for NavigationBar, and NO UIBlurEffect for StatusBar
In order to Status Bar not remain gray, I tried to do it, but to no avail =(
bounds.offsetBy(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
Also in AppDelegate (he application written in Objective-C) in didFinishLaunchingWithOptions, I tried to add it all remains the same without any changes:
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc]init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc]init]];
[[UINavigationBar appearance] setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f]];
[[UINavigationBar appearance] setTranslucent:YES];
Please help solve the problem, I mess around with it for 3 days.
Sorry for my bad English.
Setting the background image to the navigation bar does the trick. In swift 3 the below code works for me.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
output:
Going off #Vignesh answer it's a bad idea to hard code -10. For example this won't be sized correctly for iphone X.
// Find size for blur effect.
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let bounds = self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
// Create blur effect.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds
// Set navigation bar up.
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
I would also recommend creating a subclass of UINavigationController as it's good practice. Something like this:
final class func MyCustomNavigation: UINavigationController {
override func viewDidLoad() {
// Find size for blur effect.
let statusBarHeight = UIApplication.shared.statusBarFrame.size.height
let bounds = navigationBar.bounds.insetBy(dx: 0, dy: -(statusBarHeight)).offsetBy(dx: 0, dy: -(statusBarHeight))
// Create blur effect.
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds
// Set navigation bar up.
navigationBar.isTranslucent = true
navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationBar.addSubview(visualEffectView)
navigationBar.sendSubview(toBack: visualEffectView)
}
}
Swift 5, iOS 13 +
You can use UINavigationBarAppearance() in AppDelegate file. And don't forget set appearance for not scrolling mode. For me it works perfect with navigationBar.prefersLargeTitles.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.clear
appearance.backgroundEffect = UIBlurEffect(style: .light) // or dark
let scrollingAppearance = UINavigationBarAppearance()
scrollingAppearance.configureWithTransparentBackground()
scrollingAppearance.backgroundColor = .white // your view (superview) color
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollingAppearance
UINavigationBar.appearance().compactAppearance = scrollingAppearance
return true
}
Also you can check this link with test project https://github.com/leningradspb/BlurNavigationBar
Swift 3
Thanks to #Vignesh 's answer and #Romain Caron 's comment, I combined your code and the following code works for me in Swift 3
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = (self.navigationController?.navigationBar.bounds.insetBy(dx: 0, dy: -10).offsetBy(dx: 0, dy: -10))!
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.addSubview(visualEffectView)
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
Here is my Objective-C code for this same thing, and it includes support for the iPhone X's extra-large navigation bar.
UIVisualEffectView *fxView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
[fxView setFrame:CGRectOffset(CGRectInset(self.navigationController.navigationBar.bounds, 0, -12), 0, -60)];
[self.navigationController.navigationBar setTranslucent:YES];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
[self.navigationController.navigationBar insertSubview:fxView atIndex:1];
I have been trying to get an effect of having my scrollView appear under the navigationBar slightly blurred as is the case in the apple messages app. I have tried the following methods and none of them has worked:
1:
self.navigationController?.navigationBar.isTranslucent = true
2:
self.navigationController?.navigationBar.barStyle = UIBarStyle.blackTranslucent
3:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
4:
self.navigationController?.navigationBar.backgroundColor = UIColor.clear()
I have read many posts with variations of the above methods and have tried most of them but to no avail. I thought the point of setting isTranslucent = true was to get exactly that effect. Is there something else I should be trying? Basically I just want my navigationBar to be slightly see-through.
UIVisualEffectView is what you are looking for, and well demonstrated here:
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
self.navigationController?.navigationBar.addSubview(visualEffectView)
That said, use your method 1 and 2 together should also work:
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.topItem.title = #"Contacts";
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
Here you can add this code for your navigation bar blur effect this function is perfect for you
func navigationBlurEffect() {
// Add blur view
let bounds = self.navigationController?.navigationBar.bounds as CGRect!
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
//here you can choose one
self.navigationController?.navigationBar.addSubview(visualEffectView)
// Or
/*
If you find that after adding blur effect on navigationBar, navigation buttons are not visible then add below line after adding blurView code.
*/
self.navigationController?.navigationBar.sendSubviewToBack(visualEffectView)
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
//**This is only for Image Blur code
And this code you can use for Image Blur effect
func getImageBlur(image:UIImageView){
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (image.bounds)
blurEffectView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
image.addSubview(blurEffectView)
}
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = (self.navigationController?.navigationBar.bounds)!
self.navigationController?.navigationBar.addSubview(blurEffectView)
How to achieve the real time blurring effect for the navigation bar just like the Trailers app in iPhone.
i.e As you scroll the contents should get blurred behind the navigation bar.
Please help me with some code.
Thanks!
I want to achieve an effect like this:-
Apple has introduced new classes UIVisualEffectView and more to add translucency and blur effect on views from iOS 8.0 release.
Here how you can use it to add a blur effect to navigation bar or any other UIView:
Swift 5
func addBlurEffect() {
let bounds = self.navigationController?.navigationBar.bounds
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
visualEffectView.frame = bounds ?? CGRect.zero
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in the above code to add effects to the custom view.
}
Objective C Code:
- (void) addBlurEffect {
// Add blur view
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];
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in the above code to add effects to the custom view.
}
UPDATE:
If you find that after adding blur effect on navigationBar, navigation buttons are not visible then add below line after adding blurView code.
Swift:
self.navigationController?.navigationBar.sendSubview(toBack: visualEffectView)
Objective C:
[self.navigationController.navigationBar sendSubviewToBack:visualEffectView];
Swift 4
extension UINavigationBar {
func installBlurEffect() {
isTranslucent = true
setBackgroundImage(UIImage(), for: .default)
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
var blurFrame = bounds
blurFrame.size.height += statusBarHeight
blurFrame.origin.y -= statusBarHeight
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
blurView.isUserInteractionEnabled = false
blurView.frame = blurFrame
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(blurView)
blurView.layer.zPosition = -1
}
}
Usage
navigationController?.navigationBar.installBlurEffect()
Noted: on iOS 11, function sendSubviewToBack does not work normally. In order to achieve that, we should use zPosition to place the blur effect view under other views.
self.visualEffectView.layer.zPosition = -1;
Objective-C code
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
self.navigationController.navigationBar.translucent = YES;
// Add blur view
CGRect bounds = self.navigationController.navigationBar.bounds;
bounds.size.height += 20;
bounds.origin.y -= 20;
_visualEffectView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
self.visualEffectView.frame = bounds;
self.visualEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.visualEffectView.userInteractionEnabled = NO;
self.visualEffectView.layer.zPosition = -1;
[self.navigationController.navigationBar addSubview:self.visualEffectView];
[self.navigationController.navigationBar sendSubviewToBack:self.visualEffectView];
Swift 4 code
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .light))
var bounds = view.bounds
bounds.size.height += 20
bounds.origin.y -= 20
visualEffectView.isUserInteractionEnabled = false
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.navigationController?.navigationBar.addSubview(visualEffectView)
visualEffectView.layer.zPosition = -1
I've added #Kampai's,#Damasio's with my tweaks to resolve my issues(which was pushNavigation related).Code will support Swift 4.0+, iOS9, Xcode 9
In your ViewController's ViewDidLoad(), just call
addBlurEffect(toView: self.navigationController?.navigationBar)
function:
//MARK :- It can be used in navBarGlassEffect view
func addBlurEffect(toView view:UIView?) {
// Add blur view
guard let view = view else { return }
//This will let visualEffectView to work perfectly
if let navBar = view as? UINavigationBar{
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
}
var bounds = view.bounds
bounds.offsetBy(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
let blurEffect = UIBlurEffect(style: .dark)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
visualEffectView.isUserInteractionEnabled = false
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.insertSubview(visualEffectView, at: 0)
}
If, after #Kampai answer you get the status bar not getting in the effect, add this:
bounds.offsetInPlace(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
Question addressed in this topic.
Swift 5, iOS 13 +
You can use UINavigationBarAppearance() in AppDelegate file. And don't forget set appearance for not scrolling mode. For me it works perfect with navigationBar.prefersLargeTitles.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.clear
appearance.backgroundEffect = UIBlurEffect(style: .light) // or dark
let scrollingAppearance = UINavigationBarAppearance()
scrollingAppearance.configureWithTransparentBackground()
scrollingAppearance.backgroundColor = .white // your view (superview) color
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollingAppearance
UINavigationBar.appearance().compactAppearance = scrollingAppearance
return true
}
SWIFT 3:
func addBlurEffect(toView view:UIView?) {
// Add blur view
guard let view = view else { return }
//This will let visualEffectView to work perfectly
if let navBar = view as? UINavigationBar{
navBar.setBackgroundImage(UIImage(), for: .default)
navBar.shadowImage = UIImage()
}
var bounds = view.bounds
bounds.offsetBy(dx: 0.0, dy: -20.0)
bounds.size.height = bounds.height + 20.0
let blurEffect = UIBlurEffect(style: .dark)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
visualEffectView.isUserInteractionEnabled = false
visualEffectView.frame = bounds
visualEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.insertSubview(visualEffectView, at: 0)
}
I first added addBlurEffect() method and then in AppDelegate, I added
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: .Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().backgroundColor = UIColor.clearColor()
UINavigationBar.appearance().translucent = true
Now it works for me
Key note: after u implement above code to add blur view,
1. U need to send your blur view to back to show other things
2. U need to set your blur view user interaction to be false to be able to tap the items on the navigation bar.
iOS NavigationBar Blur Effect
let navigationBar = self.navigationController?.navigationBar
if #available(iOS 13.0, *) {
navigationBar?.standardAppearance.backgroundColor = .clear
navigationBar?.standardAppearance.backgroundEffect = UIBlurEffect(style: .systemMaterial)
}
Don't forget to align your UITableView to superview(not Safe area)
This is neoneye's solution from above, which works perfectly, applied to a UIToolbar.
extension UIToolbar {
func toolBarBlurEffect() {
isTranslucent = true
setBackgroundImage(UIImage(), forToolbarPosition: .any, barMetrics: .default)
let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.height
var blurFrame = bounds
blurFrame.size.height += statusBarHeight
let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
blurView.isUserInteractionEnabled = false
blurView.frame = blurFrame
blurView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
addSubview(blurView)
blurView.layer.zPosition = -1
}
}
Usage is similar:
navigationController?.toolbar.toolBarBlurEffect()