How to create navigation bar translucent effect? - swift

I have the table view with headers. Headers have only custom text and white background. Headers are pinned on top when scrolling and I want to know, is there any way how to make the background of headers translucent same as navigation bar is? With some blur effect?
Thanks for any advice!

Change the backgroundColor to .clear, add a UIVisualEffectView below the label and set the constraints to the edges. Not sure if it will mimic the behaviour of navbar by default so you would have to play around a little with the UIBlurEffect options to get the desired effect.
let blurEffectStyle = UIBlurEffectStyle.extraLight
let blurEffect = UIBlurEffect(style: blurEffectStyle)
let visualEffectView = UIVisualEffectView(effect: blurEffect)
Then add to view and set the constraints

self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.view.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.backgroundColor = UIColor.clear

Related

White line under navigation bar when scrolling or until pressing on the search bar

As you can see in the video, there is a white thin line under the navigation bar the moment i start scrolling.
It would only disappear when I would press on the searchBar (contained by my searchController, so the search bar is not added from the Storyboard). I tried a lot of different combinations in order to try to make it disappear but nothing worked.
Any help is appreciated! Thanks!
Video: https://www.youtube.com/watch?v=KcgZmBg1VS0
This is the code inside my viewDidLoad:
searchController.searchBar.delegate = self
searchController.dimsBackgroundDuringPresentation = false
searchController.hidesNavigationBarDuringPresentation = false
searchController.searchBar.placeholder = "Search for a recipe"
searchController.searchBar.barTintColor = navigationController?.navigationBar.barTintColor
searchController.searchBar.tintColor = UIColor.white
Try to set background color and change shadow of the navigationBar. It possibly connected with navigationBar.
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
self.navigationController?.navigationBar.shadowImage = UIImage()
Try to add this line:
searchController.searchBar.backgroundColor = navigationController?.navigationBar.barTintColor
In the file setupSearchBar()
After code fix:
You can remove that shadow below your navigation bar by the way. I have created extension for doing this:
extension UINavigationBar {
func shouldRemoveShadow(_ value: Bool) -> Void {
self.setValue(value, forKey: "hidesShadow")
}
}

Bringing a subview to the front of a UIVisualEffect view?

I've got a view at the top/bottom of my screen that's a blurred view. Ontop of the view I'm wanting to show my buttons unblurred, so I would assume the buttons need to be ontop of the blurred view.
in viewDidLoad I'm running this code :
if !UIAccessibilityIsReduceTransparencyEnabled() {
self.blurOutletTop.backgroundColor = UIColor.clear
self.blueOutletBot.backgroundColor = UIColor.clear
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
let blurEffectView2 = UIVisualEffectView(effect: blurEffect)
//always fill the view
blurEffectView.frame = blurOutletTop.frame //self.view.bounds
blurEffectView2.frame = blueOutletBot.frame
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)
self.view.addSubview(blurEffectView2)
logoOutlet.bringSubview(toFront: self.view) //This is where my issue is
} else {
blurOutletTop.backgroundColor = UIColor.black
blueOutletBot.backgroundColor = UIColor.black
}
So this makes the two views I have top/bottom blurry. Where I'm running into an issue is where I'm trying to have my logoOutlet infront of the blurred view so it doesn't get blurred. I've tried brining it to the front of the blurrEffectView, the blurOutletTop view, and self.view and everything has the same effect.
I wouldn't assume a button that is not behind a blur view would get effected by the blur. How do I get this to work? I'm also unable to even press the buttons that are now under the view so bringing it to the front doesn't seem to be working. (logoOutlet is an outlet for a button).
edit: I also have tried setting the outlet's zPosition to 9999, no change.

Swift Navigation bar background color white does not work

Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
Above line doesnt work just for white.
Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...
Please advice where I am going wrong...
Thanks.
Try this Code:
In your viewDidLoad:
title = "Some Title"
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
//Line under NavigationBar
let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
barView.backgroundColor=UIColor.red // set any colour you want..
navigationController?.navigationBar.addSubview(barView)
//Title Colour
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
Note:
Above code works on any background colour....
If you want the navigationBar to be green....set your view background colour to green...

Why I cannot make my UITabBarController blurred?

I have a UITabBar and I want to make it blurred. I wrote the following code:
import UIKit
class TabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
blurView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.layer.insertSublayer(blurView, atIndex: 0)
}
}
but somehow the last line throws error:
Cannot convert value of type 'UIVisualEffectView' to expected argument
type 'CALayer'
how can I fix that?
I changed the last line to:
self.tabBar.addSubview(blurView)
but now the whole tabbar is blurred (even with icons and they are not visible). When I changed this line to:
self.tabBar.sendSubviewToBack(blurView)
then the tabbar is visible, but not blurred. I want to achieve effect from accepted answer from here Black background on transparent UITabBar but here it is uitabbar and I'm using uitabbarcontroller... Can you help me with applying blur in my case?
You just add the blur view as a subview:
self.view.addSubview(blurView)
Since you just want to blue the tab bar and this class is a tab bar controller, you can do:
self.tabBar.addSubview(blueView)
You also need to change the frame:
blurView.frame = self.tabBar.bounds
why don't you just use the barTintColor property on your TabBarController?
self.tabBar.translucent = true
self.tabBar.barTintColor = UIColor.blackColor()
You don't even need to subclass UITabBarController. You can call this on any UIViewController.
self.tabBarController?.tabBar.translucent = true
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
If I understood correctly from the following comment that you posted, you want to change the UITabBar to be black in colour but still blurred.
And yes, I noticed that the UITabBarController is blurred by default, but I would like to make it blurred with specific style (.Dark).
Doing this since iOS 7 has actually become quite easy. Simply change the barStyle of your UITabBar to .black. Put the following code in your UIViewController's viewDidLoad method (note that UITabBar is translucent by default, so you don't need to specify that again).
tabBarController?.tabBar.barStyle = .black
If you want to set it back to the regular, white barStyle, change it back to .default.
tabBarController?.tabBar.barStyle = .default
You may even do this from within Interface Builder by selecting the Tab Bar in your UITabBarController's hierarchy and changing its Style to Black.
I have a solution, all you need is configure your UITabBar as following:
// next code will make tabBar fully transparent
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage() // add this if you want remove tabBar separator
tabBar.barTintColor = .clear
tabBar.backgroundColor = .black // here is your tabBar color
tabBar.layer.backgroundColor = UIColor.clear.cgColor
If you want to add blur, do this:
let blurEffect = UIBlurEffect(style: .dark) // here you can change blur style
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = tabBar.bounds
blurView.autoresizingMask = .flexibleWidth
tabBar.insertSubview(blurView, at: 0)
As a result:
Attach bottom constraint to the bottom of the view instead of Safe Area
It just might not be a problem with your TabBar but with tableView constraints.
Tab bar is blurred by default.

The title of my UIButton with UIVibrancyEffect is not visible when backgroundColor is assigned

I have two buttons added to UIVisualEffectViewwith effect UIVibrancyEffect:
let vibrancyEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .Dark)))
vibrancyEffectView.contentView.addSubview(rightActionButton)
vibrancyEffectView.contentView.addSubview(leftActionButton)
the nest is set Title for them:
rightActionButton.setTitle(rightAction.title, forState: .Normal)
leftActionButton.setTitle(leftAction?.title, forState: .Normal)
And backgroundColor:
leftActionButton.backgroundColor = UIColor.clearColor()
rightActionButton.backgroundColor = UIColor.whiteColor()
Why I cannot see the right title? How to fix this?
Below is example from Apple, exactly what I need to do:
Is it possible?
Just set a backgroundColor with alpha component.
let vibrancyEffectView = UIVisualEffectView(effect: UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .Dark)))
leftActionButton.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
rightActionButton.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(0.2)
And the result is following:
The title is there but you cannot see it because it has the same color as it background. To use UIVibrancyEffect button background color must be set to clear color so this way all transparent pixels that are multiply with their corresponding background pixels will result in transparent as well. To fix this:
rightActionButton.backgroundColor = UIColor.clearColor()