Why I cannot make my UITabBarController blurred? - swift

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.

Related

How to make UINavigationBar shadowImage fixed?

I have a reversed UITableView:
tableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(Double.pi))
NavigationBar setup:
navigationBar.shadowImage = UIImage()
navigationBar.layer.masksToBounds = false
navigationBar.layer.shadowColor = Assets.Colors.black.color.withAlphaComponent(0.2).cgColor
navigationBar.layer.shadowOpacity = 0.7
navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2.0)
navigationBar.layer.shadowRadius = 5
navigationBar.isTranslucent = false
view.backgroundColor = color
Whenever I scroll to the bottom of my TableView (to the top when inverted), shadow disappears. I want it to be constant (like at the second screenshot).
Is it possible to make UINavigationBar shadow static regardless of TableView scrolling?
How it looks now:
No shadow
How it should be:
With shadow
In iOS15/Xcode13, there is a common problem with the navigation bar's appearance. You can read more here and apply the fixes.

How to make UITabBar blurry, in Swift

I am trying to make UITabBar look blur.
I am trying to make something like this in this image
But my view now looks like this
This is my view for tabbar
I tried this code in UITabbarController -
Code:
class TabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
configureTabbar()
}
func configureTabbar(){
let blurEffect = UIBlurEffect(style: .dark)
let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
let vibrancyView = UIVisualEffectView()
vibrancyView.frame = tabBar.bounds
vibrancyView.autoresizingMask = .flexibleWidth
vibrancyView.effect = vibrancyEffect
tabBar.insertSubview(vibrancyView, at: 0)
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.backgroundColor = .clear
tabBar.barStyle = UIBarStyle.black
tabBar.barTintColor = UIColor.clear
}
System bars such as UINavigationBar, UITabBar & UIToolbar are translucent by default and you don't need to add anything extra to get that effect.
You just need to make sure that your view extends it's content under system bars. You can go to storyboard and make sure the Extend Edges - Under Bottom Bars is checked for your UIViewController that you plan to see this effect on.

Erase borders between navigation bar and searchBar swift 4

I am setting both the navigation bar and the search bar to a custom UIColor (which I call categoryColor in my code). When I do that, I still see an upper grayish line between nav bar and search bar. I have already set the searchBar border color to be the same as the others, but that gray line still exists. Does anyone know how to get rid of it?
Here is my code:
override func viewWillAppear(_ animated: Bool) {
//defining the color that will be used for all the items
let categoryColor = UIColor(hexString: selectCategory?.categoryColorHex ?? UIColor.randomFlat.hexValue())
//changing navigation bar tint color
navigationController?.navigationBar.barTintColor = categoryColor
//changing searchbar tint color
searchBar.barTintColor = categoryColor
//change searchBar border's color
searchBar.layer.borderColor = categoryColor?.cgColor
searchBar.layer.borderWidth = 3
//changing title that appears at the top, after list is loaded
title = selectCategory?.listName ?? "Todoey"
}
Here is a picture of what I see when I run the simulation:
The better way for implementing search bar with nav controller would be to use searchController inside a navigationController so the searchController will have the same background as navigationController. Here’s a great tutorial about that: https://m.youtube.com/watch?v=h7caxpp3Xps
Edit:
Also if you already implemented search capabilities you can do that with searchController too. Just set navigationController.searchConroller.searchBar.delegate for class that’s responsible for handling delegate methods

How to create navigation bar translucent effect?

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

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.