简体   繁体   English

在透明UITabBar的黑背景

[英]Black background on transparent UITabBar

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. 我试图为我的UITabViewController创建一个模糊的背景UITabBar ,并且想法是让它变得模糊和透明,以便可以看到下面的视图滚动。

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). 如果我将UITabBar的alpha更改为低,我可以看到tableview确实在它后面,但是你可以看到UITabBar有某种背景,阻止了tableview完全显示(我不知道)我想要禁止按钮项目不可见,只需标签栏背景)。

alpha 0.3标签栏

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: 在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

...yeah It's excessive but I want to try everything. ...是的它太过分但我想尝试一切。

Any ideas on what to do? 关于该怎么做的任何想法?

Make a UITabBar transparent 使UITabBar透明

Assign a clear image to its backgroundImage . 为其backgroundImage指定一个清晰的图像。 You can use a 1x1 clear.png, or create one programmatically: 您可以使用1x1 clear.png,或以编程方式创建一个:

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

This will make the UITabBar transparent: 这将使UITabBar透明:

透明

Add a blur effect 添加模糊效果

Insert a UIVisualEffectView as the rearmost subview. 插入UIVisualEffectView作为最后面的子视图。

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

This will insert a UIBlurEffect (frost): 这将插入一个UIBlurEffect (霜冻):

霜

Example

冷淡的工具栏


  1. Set the Custom Class for the UITabBar of the Tab Bar Controller to FrostyTabBar . 将Tab Bar Controller的UITabBarCustom Class设置为FrostyTabBar
  2. You have a few options to supply a clearColor image. 您有几个选项可以提供clearColor图像。 You can create a clear.png image with an alpha of 0. A programmatic elegant solution is described here . 您可以创建一个alpha为0的clear.png图像。 这里描述了一个程序化的优雅解决方案。
  3. If using a clear.png , assign it to the Background Image in the Attribute Inspector : 如果使用clear.png ,请将其分配给属性检查器中背景图像 在此输入图像描述
  4. In Interface Builder, pick Style : Default & Translucent . 在Interface Builder中,选择StyleDefaultTranslucent
  5. Once you take control of the background blur with a UIVisualEffectView , you can in turn supply any UIVisualEffect you so desire. 一旦你用UIVisualEffectView控制背景模糊,你就可以反过来提供你想要的任何UIVisualEffect

The entire FrostyTabBar class looks like this: 整个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)
    }
}

► Find this solution on GitHub and additional details including a 1x1 clear.png on Swift Recipes . ►在GitHub上找到此解决方案以及其他详细信息,包括Swift Recipes上的1x1 clear.png。

I found a prefect solution, you only need to subclass UITabBar and then do the following actions to clean that annoying views 我找到了一个完美的解决方案,你只需要子类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
                }
            }
        }
    }
}

imageWithColor was replaced by jotImage imageWithColor改为jotImage

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

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 tabbar:半透明 - > true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM