上周我一直在努力寻找解决方案,但在尝试了我能找到或想到的所有可能的解决方案后,我没有运气。我发现并尝试过的每个解决方案要么不起作用,要么已经过时。
我有 5 个UITabBarItem
's in a UITabBar
放置在UITabBarController
。我想改变背景颜色UITabBarItem
当它被选择时,当然当所选项目改变时它会变回来。
我在 Xcode 6.3.1 中使用 Swift 和 iOS SDK 8.3。如果你只能用 Objective-C 来回答也没关系,任何答案都会有帮助!预先感谢大家,我真的很感激!
编辑:这是我希望它做什么的直观示例。
不同的背景颜色 https://i.stack.imgur.com/CfIaO.jpg
在你的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
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)