在透明UITabBar上的黑色背景

我想为我的UITabViewController制作一个模糊的背景UITabBar ,并且这个想法是让它变得模糊和透明,以便下面的视图可以看到滚动。

不幸的是,我不能让我的生活得到标签栏是透明的。 不pipe我做什么,总是有一些黑色背景的标签栏,防止底层视图控制器显示。

如果我将UITabBar的alpha值改为低,我可以看到tableview确实在它后面,但是你可以看到UITabBar有一些背景,阻止了tableview完全显示(而且我不'要禁止button的项目是不可见的,只是标签栏背景)。

alpha 0.3标签栏

怎么会这样?

在自定义标签栏的视图没有加载我有:

 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 

并在AppDelegate中我有:

 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 

…是的这是过度的,但我想尝试一切。

有什么想法做什么?

使UITabBar透明

为其backgroundImage分配一个清晰的图像。 您可以使用1×1的clear.png,或以编程方式创build一个:

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

这将使UITabBar透明:

透明

添加模糊效果

插入一个UIVisualEffectView作为最后面的子视图。

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

这将插入一个UIBlurEffect (霜):

霜

冷淡的工具栏


  1. 将选项卡栏控制器的UITabBar自定义类设置为FrostyTabBar
  2. 你有几个选项来提供一个clearColor图像。 您可以创build一个alpha为0的clear.png图像。 这里描述一个编程优雅的解决scheme。
  3. 如果使用clear.png ,则将其分配给属性检查器中背景图像在这里输入图像说明
  4. 在Interface Builder中,selectStyleDefaultTranslucent
  5. 一旦你用UIVisualEffectView控制了背景模糊,你可以依次提供你所需要的任何UIVisualEffect

整个FrostyTabBar类看起来像这样:

 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) } } 

►在GitHub上find这个解决scheme,并在Swift Recipes上find 1×1的clear.png。

我find了一个完美的解决scheme,你只需要UITabBar的子类,然后执行以下操作来清除那些烦人的视图

 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 } } } } }