2015-04-26 7 views

risposta

13

Il seguente imposta i valori predefiniti per tutti gli UITabBarItem, è possibile aggiungerlo al numero AppDelegate. Cambierà il colore del tuo testo.

UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.blackColor()}, forState:.Selected) 
UITabBarItem.appearance().setTitleTextAttributes({NSForegroundColorAttributeName: UIColor.whiteColor()}, forState:.Normal) 

Per cambiare l'icona' di colore è possibile impostare l'immagine per lo stato data in cui l'immagine ha già il buon colore.

self.tabBarItem.selectedImage = [[UIImage imageNamed:@"selectedImage"] 
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 

self.tabBarItem.image = [[UIImage imageNamed:@"notSelectedImage"] 
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; 

Oppure si può fare in questo modo:

aggiungere un'estensione al UIImage classe (da this answer):

extension UIImage { 
func imageWithColor(color1: 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, kCGBlendModeNormal) 

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

    let newImage = UIGraphicsGetImageFromCurrentImageContext() as UIImage 
    UIGraphicsEndImageContext() 

    return newImage 
} 
} 

E nel tuo viewDidLoad:

for item in self.tabBar.items as [UITabBarItem] { 
    if let image = item.image { 
     item.image = image.imageWithColor(UIColor.blackColor()).imageWithRenderingMode(.AlwaysOriginal) 
    } 
} 
1

Complementare @ Risposta di BoilingLime, qui si passa al secondo alternat estensione UIImage di ive in Swift 3:

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

    let context = UIGraphicsGetCurrentContext()! as CGContext 
    context.translateBy(x: 0, y: self.size.height) 
    context.scaleBy(x: 1.0, y: -1.0); 
    context.setBlendMode(CGBlendMode.normal) 

    let rect = CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height) 
    context.clip(to: rect, mask: self.cgImage!) 
    color1.setFill() 
    context.fill(rect) 

    let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage 
    UIGraphicsEndImageContext() 

    return newImage 
} 
} 
11

iOS 10

class TabBarVC: UITabBarController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // make unselected icons white 
     self.tabBar.unselectedItemTintColor = UIColor.white 
    } 
} 
+0

si può mettere in AppDelegate, anche. –

+2

e come posso utilizzarlo per iOS 9? –

Problemi correlati