簡體   English   中英

在透明UITabBar的黑背景

[英]Black background on transparent UITabBar

我試圖為我的UITabViewController創建一個模糊的背景UITabBar ,並且想法是讓它變得模糊和透明,以便可以看到下面的視圖滾動。

不幸的是,我不能為我的生活讓標簽欄變得透明。 無論我做什么,標簽欄總會有一些黑色背景,阻止底層視圖控制器顯示。

如果我將UITabBar的alpha更改為低,我可以看到tableview確實在它后面,但是你可以看到UITabBar有某種背景,阻止了tableview完全顯示(我不知道)我想要禁止按鈕項目不可見,只需標簽欄背景)。

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指定一個清晰的圖像。 您可以使用1x1 clear.png,或以編程方式創建一個:

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. 將Tab Bar Controller的UITabBarCustom Class設置為FrostyTabBar
  2. 您有幾個選項可以提供clearColor圖像。 您可以創建一個alpha為0的clear.png圖像。 這里描述了一個程序化的優雅解決方案。
  3. 如果使用clear.png ,請將其分配給屬性檢查器中背景圖像 在此輸入圖像描述
  4. 在Interface Builder中,選擇StyleDefaultTranslucent
  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上找到此解決方案以及其他詳細信息,包括Swift Recipes上的1x1 clear.png。

我找到了一個完美的解決方案,你只需要子類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改為jotImage

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

對我有用的唯一解決方案是:

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

並設置:(你也可以在故事板中這樣做)

UITabBar.appearance().barTintColor = UIColor.clear

但我必須在故事板中設置的是:

  • tabbar:半透明 - > true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM