Ich habe in der letzten Woche versucht, die Lösung dafür zu finden, und ich hatte kein Glück, nachdem ich jede mögliche Lösung ausprobiert hatte, die ich finden konnte. Jede Lösung, die ich gefunden und versucht habe, hat entweder nicht funktioniert oder war veraltet.
Ich habe 5 UITabBarItem
s in einem UITabBar
, die sich innerhalb von UITabBarController
befinden. Ich möchte die Hintergrundfarbe von UITabBarItem
ändern, wenn es ausgewählt ist, und es natürlich ändern, wenn sich das ausgewählte Element ändert.
Ich verwende Swift und iOS SDK 8.3 in Xcode 6.3.1. Wenn Sie nur in Objective-C antworten können, ist das auch in Ordnung, jede Antwort wird helfen! Vielen Dank im Voraus, ich schätze es sehr!
EDIT: Hier ist ein visuelles Beispiel für das, was ich möchte.
In Ihrem tabBarController können Sie die Standard-UITabBar tintColor, barTintColor, selectionIndicatorImage (hier ein bisschen schummeln) und den RenderingMode der Bilder festlegen.
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)
}
}
}
...
}
Und Sie möchten die UIImage-Klasse erweitern, um das einfarbige Bild in der gewünschten Größe zu erzeugen:
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
}
}
Sie können dies versuchen. Fügen Sie dies in AppDelegate.Swift
hinzu.
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
}
Vergessen Sie nicht, diese Bibliothek einzuschließen. https://github.com/yeahdongcn/UIColor-Hex-Swift
Inspiriert von Gwendle, so habe ich es gelöst:
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)
}
Und auch die Erweiterung verwendet:
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
}
Denken Sie daran, dass nach dem Einstellen von selectionIndicationImage
alle anderen Registerkarten eingestellt bleiben. Hier ein Beispiel, wie Sie es entfernen können, indem Sie es in den anderen Registerkarten auf allen anderen Ansichtskontrollern auf null setzen:
override func viewWillAppear(animated: Bool) {
tabBarController?.tabBar.tintColor = UIColor.redColor()
tabBarController?.tabBar.selectionIndicatorImage = nil
super.viewWillAppear(animated)
}
Implementiert mit Swift 2.
Sie können diese Funktion von jedem Controller aus aufrufen, der self.tabBarController
und jede gewünschte Farbe passiert.
Funktion:
static func customTabBar(controller: UIViewController?, backgroundColor: String, unselectedColor: String, selectedColor: String) {
if let tabBarController = controller as? UITabBarController {
tabBarController.tabBar.barTintColor = UIColor(hex: backgroundColor)
tabBarController.tabBar.tintColor = UIColor(hex: selectedColor)
tabBarController.tabBar.isTranslucent = false
tabBarController.tabBar.selectedItem?.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor(hex: selectedColor)], for: UIControl.State.selected)
if #available(iOS 10.0, *) {
tabBarController.tabBar.unselectedItemTintColor = UIColor(hex: unselectedColor)
} else {
// Fallback on earlier versions
}
}
}
Hast du das probiert?
Wählen Sie in Ihrem Ansichts-Controller im Storyboard das Symbol für die Registerkartensymbole aus.
Schauen Sie auf der Registerkarte Identität und Typ (ganz links) (es sieht aus wie ein Stück Papier) im rechten Bereich von xcode.
Suchen Sie nach der globalen Farbtoneinstellung.