透明UITabBar上的黑色背景

时间:2015-11-20 05:49:45

标签: ios uitabbarcontroller ios9

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

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

如果我将UITabBar的alpha更改为低级,我可以看到tableview确实在它背后,但是你可以看到UITabBar有某种背景阻止它从完全显示的桌面视图(我不想要禁止按钮项目,只是标签栏背景)。

alpha 0.3 tab bar

这怎么可能?

在自定义标签栏的视图中加载了我:

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

...是的它太过分但我想尝试一切。

关于该怎么做的任何想法?

4 个答案:

答案 0 :(得分:43)

使UITabBar透明

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

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

这会使UITabBar透明:

Transparent

添加模糊效果

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

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

这将插入UIBlurEffect(霜冻):

Frost

示例

Frosty toolbar

  1. 将标签栏控制器的UITabBar自定义类设置为FrostyTabBar
  2. 您可以选择提供clearColor图片。您可以创建一个alpha为0的 clear.png 图像。程序化的优雅解决方案是described here
  3. 如果使用 clear.png ,请将其指定给属性检查器中的背景图像enter image description here
  4. 在Interface Builder中,选择样式默认& 半透明
  5. 使用UIVisualEffectView控制背景模糊后,您可以反过来提供您想要的任何UIVisualEffect
  6. 整个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上找到此解决方案,其他详细信息包括Swift Recipes上的1x1 clear.png。

答案 1 :(得分:1)

我找到了一个完美的解决方案,您只需要子类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
                }
            }
        }
    }
}

答案 2 :(得分:0)

imageWithColor jotImage 替换

let size = CGSize(width: tabBar.bounds.size.width,
                  height: tabBar.bounds.size.height)
tabBar.backgroundImage = UIImage.jotImage(with: UIColor.clear, size: size)

答案 3 :(得分:0)

唯一适用于我的解决方案是:

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

并设置:(您也可以在情节提要中执行此操作)

UITabBar.appearance().barTintColor = UIColor.clear

但是我必须在情节提要中设置的是:

  • 标签栏:半透明-> true