Pab*_*blo 57 objective-c uitabbarcontroller uitabbaritem uitabbar ios
如何更改iOS 7标签栏上的非活动图标/文本颜色?灰色的那个.

Gab*_*ana 87
在每个TabBar的每个第一个ViewController中:
- (void)viewDidLoad
{
[super viewDidLoad];
// changing the unselected image color, you should change the selected image
// color if you want them to be different
self.tabBarItem.selectedImage = [[UIImage imageNamed:@"yourImage_selectedImage"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.image = [[UIImage imageNamed:@"yourImage_image"]
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
Run Code Online (Sandbox Code Playgroud)
这段代码的线索是'UIImageRenderingModeAlwaysOriginal':
Apple文档的渲染模式:
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information
Run Code Online (Sandbox Code Playgroud)
要更改文字颜色:
在AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Add this if you only want to change Selected Image color
// and/or selected image text
[[UITabBar appearance] setTintColor:[UIColor redColor]];
// Add this code to change StateNormal text Color,
[UITabBarItem.appearance setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor greenColor]}
forState:UIControlStateNormal];
// then if StateSelected should be different, you should add this code
[UITabBarItem.appearance setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor purpleColor]}
forState:UIControlStateSelected];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
Vai*_*wad 14
用于更改tabbar的取消选择图标的颜色
对于iOS 10以下:
// this code need to be placed on home page of tabbar
for(UITabBarItem *item in self.tabBarController.tabBar.items) {
item.image = [item.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
Run Code Online (Sandbox Code Playgroud)
iOS 10以上:
// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)
Ste*_*omp 10
而是将其添加到每个UIViewController,您可以创建扩展并更改UITabBarController的外观
更改未选择的图标颜色
extension UITabBarController {
override public func viewDidLoad() {
super.viewDidLoad()
tabBar.items?.forEach({ (item) -> () in
item.image = item.selectedImage?.imageWithColor(UIColor.redColor()).imageWithRenderingMode(.AlwaysOriginal)
})
}
}
Run Code Online (Sandbox Code Playgroud)
更改所选图标颜色
let tabBarAppearance = UITabBar.appearance()
tabBarAppearance.tintColor = UIColor.blackColor()
Run Code Online (Sandbox Code Playgroud)
更改(取消)选定的标题颜色
let tabBarItemApperance = UITabBarItem.appearance()
tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.redColor()], forState: UIControlState.Normal)
tabBarItemApperance.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Edmondsans-Bold", size: 10)!, NSForegroundColorAttributeName:UIColor.blackColor()], forState: UIControlState.Selected)
Run Code Online (Sandbox Code Playgroud)
UIImage扩展
extension UIImage {
func imageWithColor(color1: UIColor) -> UIImage {
UIGraphicsBeginImageContextWithOptions(self.size, false, self.scale)
color1.setFill()
let context = UIGraphicsGetCurrentContext()
CGContextTranslateCTM(context!, 0, self.size.height)
CGContextScaleCTM(context!, 1.0, -1.0);
CGContextSetBlendMode(context!, .Normal)
let rect = CGRectMake(0, 0, self.size.width, self.size.height) as CGRect
CGContextClipToMask(context!, rect, self.CGImage!)
CGContextFillRect(context!, rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()! as UIImage
UIGraphicsEndImageContext()
return newImage
}
}
Run Code Online (Sandbox Code Playgroud)
通过仅使用appdelegate.m,可以更好地使用每个ViewController
在你的AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions功能中,试试这个.
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
// repeat for every tab, but increment the index each time
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];
// also repeat for every tab
firstTab.image = [[UIImage imageNamed:@"someImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"someImageSelected.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
Run Code Online (Sandbox Code Playgroud)
不要在每个 viewController 中为 tabBarItem 添加渲染图像代码,而是使用扩展
extension UITabBar{
func inActiveTintColor() {
if let items = items{
for item in items{
item.image = item.image?.withRenderingMode(.alwaysOriginal)
item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.green], for: .normal)
item.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在你的 UITabBarController 类中调用它,例如
class CustomTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.inActiveTintColor()
}
}
Run Code Online (Sandbox Code Playgroud)
您将得到如下输出:
注意:不要忘记将 CustomTabBarViewController 类分配给情节提要中的 TabBarController。