2015-11-07 177 views
4

我使用標籤欄,我有2個顏色問題。如何在swift中更改標籤欄的色調顏色?

第一個問題,色調顏色是灰色的,我用一些代碼將其更改爲白色,但只有當按下標籤時它纔會變爲白色。

class AppDelegate: UIResponder, UIApplicationDelegate { 

var window: UIWindow? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 

    let barColor = UIColor(red: 49/255, green: 75/255, blue: 108/255, alpha: 1.0) 
    let pressedTintColor = UIColor.whiteColor() 

    UITabBar.appearance().barTintColor = barColor 
    UITabBar.appearance().tintColor = pressedTintColor 

     return true 
} 

第二個問題,壓片的背景顏色應該改變,但它不會改變。

這是標籤欄的外觀。 enter image description here

這就是它應該看起來的樣子。 enter image description here

(1 PIC是在Xcode模擬器就像測試,第二PIC是它的設計,所以它不是重要得多有關圖片和標籤的文字)

所以它應該所有選項卡是所有時間白色,並且當按下一個標籤來改變標籤的背景顏色。

回答

7

爲了解決這個問題,你AppDelegate做到這一點:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 

    UITabBar.appearance().tintColor = UIColor.whiteColor() 
    UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState: UIControlState.Normal) 

    return true 
} 

而在你ViewController做這樣的事情:

extension UIImage { 
    func makeImageWithColorAndSize(color: UIColor, size: CGSize) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(size, false, 0) 
     color.setFill() 
     UIRectFill(CGRectMake(0, 0, size.width, size.height)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

extension UIImage { 
    func imageWithColor(tintColor: UIColor) -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale) 

     let context = UIGraphicsGetCurrentContext()! as CGContextRef 
     CGContextTranslateCTM(context, 0, self.size.height) 
     CGContextScaleCTM(context, 1.0, -1.0); 
     CGContextSetBlendMode(context, CGBlendMode.Normal) 

     let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect 
     CGContextClipToMask(context, rect, self.CGImage) 
     tintColor.setFill() 
     CGContextFillRect(context, rect) 

     let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage 
     UIGraphicsEndImageContext() 

     return newImage 
    } 
} 

class FirstViewController: UIViewController { 

    var tabBar: UITabBar? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     tabBar = self.tabBarController!.tabBar 
     tabBar!.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar!.frame.width/CGFloat(tabBar!.items!.count), tabBar!.frame.height)) 

     // To change tintColor for unselected tabs 
     for item in tabBar!.items! as [UITabBarItem] { 
      if let image = item.image { 
       item.image = image.imageWithColor(UIColor.whiteColor()).imageWithRenderingMode(.AlwaysOriginal) 
      } 
     } 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
} 

*第extensionUIImage從另一個問題被採取同一作者:How to change default grey color of tab bar items?

+0

SWIFT 3更新:http://stackoverflow.com/a/38164547/1736679 – Efren

相關問題