Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 628
user3766930 Avatar asked Oct 06 '16 17:10

user3766930


3 Answers

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
like image 56
rmaddy Avatar answered Nov 15 '22 06:11

rmaddy


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()
like image 21
ergoon Avatar answered Nov 15 '22 07:11

ergoon


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.

Tab Bar Style

like image 23
kabiroberai Avatar answered Nov 15 '22 06:11

kabiroberai