2015-11-12 102 views
1

我試圖在沒有任何成功的情況下爲我的NavigationBar添加一些透明度。在我的AppDelegate'sdidFinishLaunchingWithOptions,我有以下幾行代碼:導航欄外觀barTintColor alpha組件

let navigationBarAppearance = UINavigationBar.appearance() 
navigationBarAppearance.barTintColor = UIColor.blueColor().colorWithAlphaComponent(0.2) 
navigationBarAppearance.translucent = true 

但是,沒有母校的alpha分量是多少,透明度不改變。我甚至嘗試使用顏色UIImage

let colorImage = UIImage.imageFromColor(UIColor.blueColor().colorWithAlphaComponent(0.2), frame: CGRect(x: 0, y: 0, width: 340, height: 64)) 
navigationBarAppearance.setBackgroundImage(colorImage, forBarMetrics: .Default) 

這種方法阿洛斯能源部不會改變導航欄的透明度。我試圖在一個視圖控制器內:

self.navigationController?.navigationBar.tintColor = UIColor.blueColor() 
self.navigationController?.navigationBar.alpha = 0.2 
self.navigationController?.navigationBar.translucent = true 

仍然沒有運氣。任何人都可以幫我弄清楚如何爲我的導航欄添加透明度?

+0

導航條神奇。懷疑開發者是否知道它背後的原因。 –

回答

1

在最後一個例子,你已經改變了tintColor,而不是barTintColor。 不管怎麼說,這三條線應該工作:

self.navigationController?.navigationBar.barTintColor = UIColor.greenColor() 
self.navigationController?.navigationBar.alpha = 0.2 
self.navigationController?.navigationBar.translucent = true 

但請記住,它會改變吧爲整個導航控制器,所以你必須去從視圖控制器回來的時候去想它。

+0

alpha組件仍然沒有任何效果。我可以將其更改爲0.0,並且與0.2或甚至1.0時相同。 –

+0

它適用於示例項目,所以你可能會檢查你是否有導航控制器(不是零),或者檢查它是否在主線程上完成(你可以嘗試在viewDidLoad中做) – sunshinejr

7

在這裏你去的人。只需按照以下步驟操作

首先創建一個擴展類。只添加一個新的空迅速文件,並將它命名爲UIImageExtension或任何你覺得

寫下面的代碼在擴展

import Foundation 
import UIKit 
extension UIImage{ 
    static func imageFromColor(color: UIColor) -> UIImage { 
    let rect = CGRectMake(0, 0, 1, 1) 

    // create a 1 by 1 pixel context 
    UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) 
    color.setFill() 
    UIRectFill(rect) 

    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 

    } 
} 

現在去你的ViewController類,並用下面的代碼和遊戲與alpha。而已。

let image = UIImage.imageFromColor(UIColor(red: 0.071, green: 0.071, blue: 0.922, alpha: 0.2)) 
self.navigationController?.navigationBar.setBackgroundImage(image, forBarMetrics: UIBarMetrics.Default) 
self.navigationController?.navigationBar.barStyle = .Default 

的SWIFT 3.0

import Foundation 
import Foundation 
import UIKit 
extension UIImage{ 
    static func imageFromColor(color: UIColor) -> UIImage { 
     let rect = CGRect(x: 0, y: 0, width: 1, height: 1) 

     // create a 1 by 1 pixel context 
     UIGraphicsBeginImageContextWithOptions(rect.size, false, 0) 
     color.setFill() 
     UIRectFill(rect) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image! 

    } 
} 

USAGE

let image = UIImage.imageFromColor(color: UIColor(red: 0.071, green: 0.071, blue: 0.922, alpha: 0.2)) 
self.navigationController?.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default) 
self.navigationController?.navigationBar.barStyle = .default 

我也做了樣品。看一看。示例是Swift 2.x版本
https://github.com/RajanMaheshwari/TransparentNav
希望它有幫助!

編輯: 只是改變了導航欄半透明和它的工作 enter image description here

截圖
enter image description here

+1

@Mike Walker 它工作嗎? –