16

I am trying to make a blurred background the UITabBar for my UITabViewController, and the idea is to have it be blurred and transparent so that the views underneath can be seen scrolling by.

Unfortunately I cannot for the life of me get the tab bar to be transparent. No matter what I do, there is always some black background to the tab bar that prevents the underlying view controllers from showing through.

If I change the alpha of the UITabBar to something low I can see that the tableview is indeed behind it, however you can see that the UITabBar has some sort of background to it that is preventing the tableview from fully showing through (and I don't want to bar button items to be invisible, just the tab bar background).

alpha 0.3 tab bar

How can this be?

In the custom tab bar's view did load I have:

self.tabBar.translucent = true
self.tabBar.alpha = 0.3
self.tabBar.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.0)
self.tabBar.layer.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.0).CGColor
self.tabBar.backgroundImage = nil
self.tabBar.shadowImage = nil

and in the AppDelegate I have:

UITabBar.appearance().barTintColor = UIColor.clearColor()
UITabBar.appearance().tintColor = kColorAccent
UITabBar.appearance().translucent = true
UITabBar.appearance().translucent = true
UITabBar.appearance().backgroundColor = UIColor.clearColor()
UITabBar.appearance().backgroundImage = nil
UITabBar.appearance().layer.backgroundColor = UIColor.clearColor().CGColor
UITabBar.appearance().shadowImage = nil

...yeah It's excessive but I want to try everything.

Any ideas on what to do?

SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
Josh
  • 1,688
  • 4
  • 22
  • 35
  • have you tried this? http://stackoverflow.com/questions/20979281/ios-7-tabbar-translucent-issue – johny kumar Nov 20 '15 at 05:53
  • @johnykumar yes I have, the tab bar is set to translucent but still nothing shows up underneath. – Josh Nov 20 '15 at 20:43
  • So do you want your tab bar to be completely transparent? Or translucent with a blur? – JAL Nov 23 '15 at 19:19
  • Completely transparent with a blur. When I made the bar translucent, nothing shows through, there is that black box. I want to make it totally transparent and then add a blurred view behind it. Basically I just want it to look exactly like the app store app's tab bar. – Josh Nov 24 '15 at 00:32
  • Also don't forget to uncheck "Opaque" checkmark to make TabBar transparent: [TabBar](https://i.stack.imgur.com/Novpy.png) – andreybashta Jun 04 '19 at 19:26

3 Answers3

50

Make a UITabBar transparent

Assign a clear image to its backgroundImage. You can use a 1x1 clear.png, or create one programmatically:

self.backgroundImage = UIImage.imageWithColor(UIColor.clearColor())

This will make the UITabBar transparent:

Transparent

Add a blur effect

Insert a UIVisualEffectView as the rearmost subview.

let frost = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
frost.frame = self.bounds
self.insertSubview(frost, atIndex: 0)

This will insert a UIBlurEffect (frost):

Frost

Example

Frosty toolbar


  1. Set the Custom Class for the UITabBar of the Tab Bar Controller to FrostyTabBar.
  2. You have a few options to supply a clearColor image. You can create a clear.png image with an alpha of 0. A programmatic elegant solution is described here.
  3. If using a clear.png, assign it to the Background Image in the Attribute Inspector: enter image description here
  4. In Interface Builder, pick Style: Default & Translucent.
  5. Once you take control of the background blur with a UIVisualEffectView, you can in turn supply any UIVisualEffect you so desire.

The entire FrostyTabBar class looks like this:

import UIKit

class FrostyTabBar: UITabBar {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let frost = UIVisualEffectView(effect: UIBlurEffect(style: .light))
        frost.frame = bounds
        frost.autoresizingMask = .flexibleWidth
        insertSubview(frost, at: 0)
    }
}

► Find this solution on GitHub and additional details including a 1x1 clear.png on Swift Recipes.

Community
  • 1
  • 1
SwiftArchitect
  • 47,376
  • 28
  • 140
  • 179
  • MY MAN! Worked perfectly! I think the issue was that I was setting the background image to 'nil' which resulted in the "black" background. Setting the background image to a 1x1 clear image worked like a charm. – Josh Nov 27 '15 at 20:51
  • 2
    I can't find that method (`imageWithColor`) associated with UIImage, but I do see it with `CIImage`. – Adrian Sep 28 '17 at 11:58
  • 1
    @Adrian if download the project, you'll see the UIImage extension that creates this imageWithColor static function to use. – C0D3 Nov 08 '17 at 21:05
  • Thanks so much for the code. One thing I wanna mention is the insertSubview for the UIVisualEffectView won't work on the UITabBar.appearance because at that point the TabBar isn't actually created. This UIVisualEffectView needs to be inserted once the TabBarController is created. – C0D3 Nov 08 '17 at 21:10
  • @c0d3Junk13 downloaded but couldn't find what you mentioned – AnLT Jan 02 '18 at 04:02
  • @c0d3Junk13: Verified that [Github project](https://github.com/SwiftArchitect/SO-33819852) still works as designed when `TabBarController` uses **Custom Class** `FrostyTabBar` in Storyboard. – SwiftArchitect Feb 04 '18 at 04:27
  • 1
    What I was missing what translucent being "On" –  Jul 19 '18 at 00:31
  • Excuse me but this is not explained so far, if you use static functions past them or say it so it can be clear enough. – mehdigriche Oct 09 '21 at 07:11
1

I found a prefect solution, you only need to subclass UITabBar and then do the following actions to clean that annoying views

class MainTabBar: UITabBar {
    var cleanDone = false

    override func layoutSubviews() {
        super.layoutSubviews()
        self.deleteUnusedViews()
    }

    func deleteUnusedViews() {
        if !self.cleanDone {
            var removeCount = 0
            for (_, eachView) in (self.subviews.enumerate()) {
                if NSStringFromClass(eachView.classForCoder).rangeOfString("_UITabBarBackgroundView") != nil {
                    eachView.removeFromSuperview()
                    removeCount += 1
                }
                if NSStringFromClass(eachView.classForCoder).rangeOfString("UIImageView") != nil {
                    eachView.removeFromSuperview()
                    removeCount += 1
                }
                if removeCount == 2 {
                    self.cleanDone = true
                    break
                }
            }
        }
    }
}
Dicky Tsang
  • 6,135
  • 4
  • 20
  • 18
1

the only solution that worked for me was this:

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()

and set: (you can do this in storyboard as well)

UITabBar.appearance().barTintColor = UIColor.clear

but what i have to set in storyboard is:

  • tabbar : translucent -> true
Skyborg
  • 854
  • 13
  • 13
  • setting backgroundImage clears the background and setting shadowImage clears the line on top of the tabBar, and by setting both does the trick! thank you!!! – Bruce Dec 11 '20 at 01:39
  • @Bruce I am glad that I could help you. – Skyborg Dec 11 '20 at 16:49