IOS 8 Barre d'tabs

J'ai essayé de find la solution à cette question la semaine dernière, et je n'ai pas eu de chance après avoir essayé toutes les solutions possibles auxquelles je pouvais find ou penser. Toutes les solutions que j'ai trouvées et que j'ai essayées n'ont pas fonctionné ou ont été dépassées.

J'ai 5 UITabBarItem dans un UITabBar placé dans UITabBarController . Je veux changer la couleur d'arrière-plan de l' UITabBarItem quand il est sélectionné, et bien sûr le changer quand l'élément sélectionné change.

J'utilise Swift, et iOS SDK 8.3 dans Xcode 6.3.1. Si vous ne pouvez répondre qu'en Objective-C, c'est bien, toute réponse vous aidera! Merci d'avance, j'apprécie vraiment!

EDIT: Voici un exemple visuel de ce que je voudrais qu'il fasse.

Couleur de fond différente

Dans votre tabBarController, vous pouvez définir les valeurs par défaut UITabBar tintColor, barTintColor, selectionIndicatorImage (sortingcher un peu ici) et renderingMode des images, voir les commentaires ci-dessous:

  class MyTabBarController: UITabBarController, UINavigationControllerDelegate { ... override func viewDidLoad() { ... // Sets the default color of the icon of the selected UITabBarItem and Title UITabBar.appearance().tintColor = UIColor.redColor() // Sets the default color of the background of the UITabBar UITabBar.appearance().barTintColor = UIColor.blackColor() // Sets the background color of the selected UITabBarItem (using and plain colored UIImage with the width = 1/5 of the tabBar (if you have 5 items) and the height of the tabBar) UITabBar.appearance().selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.blueColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height)) // Uses the original colors for your images, so they aren't not rendered as grey automatically. for item in self.tabBar.items as! [UITabBarItem] { if let image = item.image { item.image = image.imageWithRenderingMode(.AlwaysOriginal) } } } ... } 

Et vous voudrez étendre la class UIImage pour faire l'image en couleur avec la taille dont vous avez besoin:

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

Vous pouvez essayer celui-ci. Ajoutez-le dans AppDelegate.swift .

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. UITabBar.appearance().translucent = false UITabBar.appearance().barTintColor = UIColor(rgba: "#12296f") UITabBar.appearance().tintColor = UIColor.whiteColor() return true } 

Ne pas oublier d'inclure cette bibliothèque. https://github.com/yeahdongcn/UIColor-Hex-Swift

Inspiré par Gwendle, voici comment je l'ai résolu:

 override func viewWillAppear(animated: Bool) { guard let tabBar = tabBarController?.tabBar else { return } tabBar.tintColor = UIColor.whiteColor() tabBar.selectionIndicatorImage = UIImage().makeImageWithColorAndSize(UIColor.redColor(), size: CGSizeMake(tabBar.frame.width/5, tabBar.frame.height)) super.viewWillAppear(animated) } 

Et aussi utilisé l'extension:

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

Gardez à l'esprit qu'après avoir défini l' selectionIndicationImage il rest défini pour tous vos autres tabs. Voici un exemple de comment l'enlever, en le mettant à zéro dans tous les autres controllers de vue dans les tabs restants:

 override func viewWillAppear(animated: Bool) { tabBarController?.tabBar.tintColor = UIColor.redColor() tabBarController?.tabBar.selectionIndicatorImage = nil super.viewWillAppear(animated) } 

Mis en œuvre en utilisant Swift 2.

Avez-vous essayé cela?

Sélectionnez l'icône de l'icône de la barre d'tabs dans votre controller de vue dans le storyboard.

Regardez dans l'onglet Identité et type (extrême gauche) (il ressemble à un morceau de papier) sur le panneau de droite de xcode.

Recherchez le paramètre de teinte globale.