如何更改iOS 7标签栏上的非活动图标/文字颜色?灰色的那个。
答案 0 :(得分:113)
您还可以直接在资产目录中设置标签栏图像的属性Render As
。在那里,您可以选择将属性设置为Default
,Original Image
和Template Image
。
答案 1 :(得分:85)
在每个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];
}
此代码的线索是'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
要更改文字颜色:
在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;
}
答案 2 :(得分:14)
用于更改标签栏的取消选择图标的颜色
对于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];
}
iOS 10以上:
// this need to be in appdelegate didFinishLaunchingWithOptions
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor blackColor]];
答案 3 :(得分: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)
})
}
}
更改所选图标颜色
let tabBarAppearance = UITabBar.appearance()
tabBarAppearance.tintColor = UIColor.blackColor()
更改(取消)选定的标题颜色
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)
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
}
}
答案 4 :(得分:7)
只使用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];
答案 5 :(得分:7)
答案 6 :(得分:4)
使用Swift 3从iOS 10+开始以编程方式执行此操作的新答案是使用unselectedItemTintColor
API。例如,如果您已在AppDelegate
内初始化了标签栏控制器,则它将如下所示:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
...
let firstViewController = VC1()
let secondViewController = VC2()
let thirdViewController = VC3()
let tabBarCtrl = UITabBarController()
tabBarCtrl.viewControllers = [firstViewController, secondViewController, thirdViewController]
// set the color of the active tab
tabBarCtrl.tabBar.tintColor = UIColor.white
// set the color of the inactive tabs
tabBarCtrl.tabBar.unselectedItemTintColor = UIColor.gray
// set the text color
...
}
用于设置所选和未选择的文本颜色:
let unselectedItem = [NSForegroundColorAttributeName: UIColor.green]
let selectedItem = [NSForegroundColorAttributeName: UIColor.red]
self.tabBarItem.setTitleTextAttributes(unselectedItem, for: .normal)
self.tabBarItem.setTitleTextAttributes(selectedItem, for: .selected)
答案 7 :(得分:3)
在Swift 3.0中,您可以按如下方式编写
对于未选中的标签栏图像
@Override
public boolean equals(Object o)
{
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Person person = (Person) o;
if (age != null ? !age.equals(person.age) : person.age != null)
return false;
return name != null ? name.equals(person.name) : person.name == null;
}
@Override
public int hashCode()
{
int result = age != null ? age.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
对于选定的标签栏图像
viewController.tabBarItem.image = UIImage(named: "image")?.withRenderingMode(.alwaysOriginal)
答案 8 :(得分:2)
您只需要将此代码放在第一个调用TabBarViewController(ViewDidload)的ViewController中:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self loadIconsTabBar];
}
-(void) loadIconsTabBar{
UITabBar *tabBar = self.tabBarController.tabBar;
//
UITabBarItem *firstTab = [tabBar.items objectAtIndex:0];
UITabBarItem *secondTab = [tabBar.items objectAtIndex:1];
firstTab.image = [[UIImage imageNamed:@"icono1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
firstTab.selectedImage = [[UIImage imageNamed:@"icono1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
secondTab.image = [[UIImage imageNamed:@"icono2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
secondTab.selectedImage = [[UIImage imageNamed:@"icono2.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
}
答案 9 :(得分:2)
我认为@ anka的答案非常好,我还添加了以下代码,以便为突出显示的项目启用色调颜色:
let image = UIImage(named:"tab-account")!.imageWithRenderingMode(.AlwaysTemplate)
let item = tabBar.items![IC.const.tab_account] as! UITabBarItem
item.selectedImage = image
或者在一行中:
(tabBar.items![IC.const.tab_account] as! UITabBarItem).selectedImage = UIImage(named:"tab-account")!.imageWithRenderingMode(.AlwaysTemplate)
所以看起来像:
答案 10 :(得分:2)
不使用tabBarItem在每个viewController中添加渲染图像代码,而是使用扩展名
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)
}
}
}
}
然后在你的UITabBarController类中调用它,如
class CustomTabBarViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
tabBar.inActiveTintColor()
}
}
您将获得如下输出: 注意:不要忘记将CustomTabBarViewController类分配给故事板中的TabBarController。
答案 11 :(得分:1)
您只需将此代码放入appDelegate.m中调用(didFinishLaunchingWithOptions):
[UITabBarItem.appearance setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor whiteColor]}
forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:
@{NSForegroundColorAttributeName : [UIColor orangeColor]}
forState:UIControlStateSelected];
[[UITabBar appearance] setTintColor:[UIColor whiteColor]]; // for unselected items that are gray
[[UITabBar appearance] setUnselectedItemTintColor:[UIColor whiteColor]];
答案 12 :(得分:1)
如果您正在寻找iOS 11 swift 4解决方案,请在appDelegate中执行类似的操作。这会将所有未选中的内容更改为黑色。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().unselectedItemTintColor = UIColor(displayP3Red: 0, green: 0, blue: 0, alpha: 1)
return true
}
答案 13 :(得分:1)
我认为是时候使用
UITabBar unselectedItemTintColor外观
/// Unselected items in this tab bar will be tinted with this color. Setting this value to nil indicates that UITabBar should use its default value instead.
@available(iOS 10.0, *)
@NSCopying open var unselectedItemTintColor: UIColor?
只需将此行添加到App中即可启动
UITabBar.appearance().unselectedItemTintColor = {your color}
// Example
UITabBar.appearance().unselectedItemTintColor = .black
答案 14 :(得分:0)
更改所选标签栏项目颜色的最佳方法是在appdelegate didFinishLaunchingWithOptions 方法
上添加单个代码UITabBar.appearance().tintColor = UIColor(red: 242/255.0, green: 32/255.0, blue: 80/255.0, alpha: 1.0)
它将更改所选项目文本颜色
答案 15 :(得分:0)
您可以通过界面构建器完成所有操作,查看此答案,它会显示如何同时执行活动和非活动,"Change tab bar item selected color in a storyboard"
答案 16 :(得分:0)
对于swift 3:
// both have to declare in view hierarchy method
//(i.e: viewdidload, viewwillappear) according to your need.
//this one is, when tab bar is selected
self.tabBarItem.selectedImage = UIImage.init(named: "iOS")?.withRenderingMode(.alwaysOriginal)
// this one is when tab bar is not selected
self.tabBarItem.image = UIImage.init(named: "otp")?.withRenderingMode(.alwaysOriginal)
答案 17 :(得分:-1)
这对我有用 SWIFT 3
viewConroller.tabBarItem = UITabBarItem(title: "", image: UIImage(named: "image")?.withRenderingMode(.alwaysOriginal),
selectedImage: UIImage(named: "image"))
答案 18 :(得分:-3)
Swift 解决方案: 对于UNSELECED项目: 在每个ViewController中 - > ViewDidLoad()
self.tabBarItem = UITabBarItem(title: nil, image: UIImage(named: "PHOTO_NAME")?.imageWithRenderingMode(.AlwaysOriginal), selectedImage: UIImage(named: "NAME"))