我一直试图在上周找到解决方案,在尝试了我能找到或想到的每一个可能的解决方案后,我没有运气。我发现并尝试过的每一个解决方案都没有工作,或者已经过时了。
我在UITabBarItem
放置的UITabBar
中有5个UITabBarController
。我想在选择时更改UITabBarItem
的背景颜色,当然,当所选项目发生变化时,它会更改。
我在Xcode 6.3.1中使用Swift和iOS SDK 8.3。如果您只能在Objective-C中回答也没问题,那么任何答案都会有所帮助!提前谢谢大家,我真的很感激!
编辑:这是我希望它做的一个视觉示例。
在tabBarController中,您可以设置默认的UITabBar tintColor,barTintColor,selectionIndicatorImage(这里有点作弊)和图像的renderingMode,请参阅下面的注释:
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)
}
}
}
...
}
并且您将需要扩展UIImage类以使您需要的大小生成纯色图像:
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
}
}
你可以尝试这个。在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
}
灵感来自Gwendle,这就是我解决它的方式:
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)
}
并且还使用了扩展名:
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
}
请记住,在设置selectionIndicationImage
后,它将保留为所有其他选项卡设置。这是一个如何删除它的示例,通过在其余选项卡中的每个其他视图控制器中将其设置为nil:
override func viewWillAppear(animated: Bool) {
tabBarController?.tabBar.tintColor = UIColor.redColor()
tabBarController?.tabBar.selectionIndicatorImage = nil
super.viewWillAppear(animated)
}
使用Swift 2实现。
你试过这个吗?
在故事板中的视图控制器中选择标签栏图标图像。
在xcode的右侧面板中查看“身份和类型”(最左侧)选项卡(看起来像一张纸)。
寻找全局色调设置。
您可以通过self.tabBarController
和您想要的每种颜色从每个控制器调用此函数。
功能:
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
}
}
}