在过去的一周里,我一直在努力寻找这个问题的解决方案,但在尝试了我能找到或想到的所有可能的解决方案后,我一直没有找到任何幸运的答案。我找到并尝试的每一个解决方案要么不起作用,要么已经过时了。
我将5个UITabBarItem放在UITabBarController中的UITabBar中。我想要在UITabBarItem被选中时更改它的背景颜色,当然,当所选项目更改时,它也会变回来。
我使用的是Swift和Xcode6.3.1中的iOS SDK8.3。如果你只能用Objective-C来回答,那也没问题,任何答案都会有帮助!提前感谢大家,我真的很感谢!
编辑:这是一个我想要它做什么的可视化示例。
Different Background Color
发布于 2015-07-28 23:28:50
在你的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
}
}发布于 2015-05-05 10:21:35
你可以试试这个。在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
}发布于 2016-03-11 22:53:52
受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实现。
https://stackoverflow.com/questions/30041127
复制相似问题