这是我的标签栏:
下图显示了正在运行的程序并选择了“ NEWS”项:
很明显,酒吧色调颜色可以按我的要求正常工作!
但是tintColor仅影响图像而不影响文本。
另外,在选择了一个项目(就像上面说的,新闻)项目颜色变蓝!如何防止这种情况发生?我希望它保持白色。
为什么在选择文本时将其更改为白色,而在未选择文本时却将其更改为白色?
我基本上希望项目颜色和文本颜色始终保持白色。
我该如何实现?谢谢你的帮助。
是否需要为每个单独的项目提供快速代码?
编辑:
这是我的标签栏:
下图显示了正在运行的程序并选择了“ NEWS”项:
很明显,酒吧色调颜色可以按我的要求正常工作!
但是tintColor仅影响图像而不影响文本。
另外,在选择了一个项目(就像上面说的,新闻)项目颜色变蓝!如何防止这种情况发生?我希望它保持白色。
为什么在选择文本时将其更改为白色,而在未选择文本时却将其更改为白色?
我基本上希望项目颜色和文本颜色始终保持白色。
我该如何实现?谢谢你的帮助。
是否需要为每个单独的项目提供快速代码?
编辑:
Answers:
从UITabBarItem类文档:
默认情况下,实际的未选择和选择的图像是根据源图像中的alpha值自动创建的。为防止系统变色,请为图像提供UIImageRenderingModeAlwaysOriginal。
线索不是您是否使用UIImageRenderingModeAlwaysOriginal,重要的是何时使用它。
为了防止未选中项目的灰色,您只需要防止未选中图像的系统着色即可。这是操作方法:
var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem
如您所见,我要求iOS仅将图像的原始颜色(白色,黄色,红色等)应用于UNSELECTED状态,而将图像保留为SELECTED状态。
另外,您可能需要为标签栏添加淡色,以便为SELECTED状态应用其他颜色(而不是默认的iOS蓝色)。根据上面的屏幕截图,您正在为选定状态应用白色:
self.tabBar.tintColor = UIColor.whiteColor()
编辑:
firstViewController.tabBarItem = customTabBarItem
TabBarClass: ??, ??
,我似乎无法将其与我的标签栏连接。我必须将其连接到标签栏或标签栏控制器吗?
迅捷3
我是通过创建自定义标签栏控制器并将其添加到viewDidLoad
方法中来完成的。
if let count = self.tabBar.items?.count {
for i in 0...(count-1) {
let imageNameForSelectedState = arrayOfImageNameForSelectedState[i]
let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
self.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
self.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
}
}
let selectedColor = UIColor(red: 246.0/255.0, green: 155.0/255.0, blue: 13.0/255.0, alpha: 1.0)
let unselectedColor = UIColor(red: 16.0/255.0, green: 224.0/255.0, blue: 223.0/255.0, alpha: 1.0)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor], for: .selected)
它为我工作!
对于图像:
custom.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "tab_icon_normal"), selectedImage: UIImage(named: "tab_icon_selected"))
对于文字:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.gray], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
对我有用的解决方案:
这将防止Tab栏组件设置其默认图像色调。
文本-在这里,我创建了一个简单的UITabBarController子类,并在其viewDidLoad方法中自定义了默认的和选定的文本颜色,如下所示:
class HomeTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarItem.appearance(whenContainedInInstancesOf: [HomeTabBarController.self])
appearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: .black], for: .normal)
appearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: .red], for: .selected)
}
}
只需在IB的身份检查器中将此类设置为Tab栏控制器自定义类即可。
瞧!而已。
iOS 13更新:
将此添加到iOS 13的设置中:
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .red]
tabBar.standardAppearance = appearance
}
迅捷3
这对我有用(指设置tabBarItems图像颜色):
UITabBar.appearance().tintColor = ThemeColor.Blue
if let items = tabBarController.tabBar.items {
let tabBarImages = getTabBarImages() // tabBarImages: [UIImage]
for i in 0..<items.count {
let tabBarItem = items[i]
let tabBarImage = tabBarImages[i]
tabBarItem.image = tabBarImage.withRenderingMode(.alwaysOriginal)
tabBarItem.selectedImage = tabBarImage
}
}
我注意到,如果使用渲染模式= .alwaysOriginal设置图像,则UITabBar.tintColor无效。
首先,确保已将BOOLEAN键“基于视图控制器的状态栏外观”添加到Info.plist,并将其值设置为“ NO”。
Appdelegate.swift
在“ launchOptions:[UIApplicationLaunchOptionsKey:Any] ??”之后的某处插入代码)-> Bool {”
UITabBar.appearance().barTintColor = UIColor(red: 0.145, green: 0.592, blue: 0.804, alpha: 1.00)
或默认的UI颜色之一:
UITabBar.appearance().barTintColor = UIColor.white)
所选项目
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: .selected)
无效项目
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.black], for: .normal)
如果您不从头开始制作图标,则在Photoshop中相对容易制作黑白交替版本。
Adobe Photoshop(几乎任何版本都可以)
确保图标图像具有透明背景,并且图标本身为纯黑色(或紧密)。
打开图像文件,将其保存为其他文件名(例如exampleFilename-Inverted.png)
在“图像”菜单上的“调整”子菜单中:
点击“反转”
现在,您的原始图标为负数。
在XCode中,在情节提要的“标签栏”属性下将其中一张图像设置为“选定的图像”,并在“栏项”图像下指定“非活动”版本。
塔达🍺
UITabBar.appearance().tintColor = UIColor.black
尝试将其添加到AppDelegate.swift(内部应用程序方法)中:
UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
// For WHITE color:
UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
例:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Tab bar icon selected color
UITabBar.appearance().tintColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
// For WHITE color: UITabBar.appearance().tintColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
return true
}
例:
我的英语太差了!对不起!:-)
我知道这里有很多答案,但是我找不到适用于Swift 4.2 / Swift 5.1的简单有效的复制/粘贴答案
tabBarController?.tabBar.tintColor = UIColor.red
tabBarController?.tabBar.unselectedItemTintColor = UIColor.green
或使用UITabBarItem.appearance()
代替tabBarController?.tabBar
斯威夫特3.0
我创建了标签栏类文件,并编写了以下代码
在viewDidLoad
:
self.tabBar.barTintColor = UIColor.white
self.tabBar.isTranslucent = true
let selectedColor = UIColor.red
let unselectedColor = UIColor.cyan
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: unselectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: selectedColor,NSFontAttributeName: UIFont(name: "Gotham-Book", size: 10)!], for: .selected)
if let items = self.tabBar.items {
for item in items {
if let image = item.image {
item.image = image.withRenderingMode( .alwaysOriginal )
item.selectedImage = UIImage(named: "(Imagename)-a")?.withRenderingMode(.alwaysOriginal)
}
}
}
之后viewDidLoad
:
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if(item.title! == "title")
{
item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
}
if(item.title! == "title")
{
item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
}
if(item.title! == "title")
{
item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
}
if(item.title! == "title")
{
item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
}
if(item.title! == "title")
{
item.selectedImage = UIImage(named: "(Imagname)-a")?.withRenderingMode(.alwaysOriginal)
}
}
在视图加载方法中,您必须设置所选图像,而其他图像则使用RenderingMode显示,并且在选项卡栏委托方法中,您可以按标题设置所选图像
对于Swift 4.0,现在更改为:
tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.gray], for: .normal)
tabBarItem.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.blue], for: .selected)
如果您只需要更改文本颜色,则不必子类化UITabBarItem。只需将以上代码放入视图控制器的viewDidLoad
函数中即可。
对于全局设置,请更改tabBarItem
为UITabBarItem.appearance()
。
在Swift 4.2中:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.red], for: .selected)
您可以设置UIBarItem的tintColor:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.magentaColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.redColor()], forState:.Selected)
您也可以通过这种方式进行:
override func viewWillLayoutSubviews() {
if let items = self.tabBar.items {
for item in 0..<items.count {
items[item].image = items[item].image?.withRenderingMode(.alwaysOriginal)
items[item].selectedImage = items[item].selectedImage?.withRenderingMode(.alwaysTemplate)
}
可选的:
UITabBar.appearance().tintColor = UIColor.red
希望对您有帮助。
//Set Tab bar text/item fonts and size
let fontAttributes = [NSAttributedString.Key.font: UIFont(name: "YourFontName", size: 12.0)!]
UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
//Set Tab bar text/item color
UITabBar.appearance().tintColor = UIColor.init(named: "YourColorName")
如果要更改按下时的图像,此代码适用于Swift 4Tab Bar Item
。复制并粘贴在viewDidLoad
项目中找到的第一个方法
let arrayOfImageNameForSelectedState:[String] = ["Image1Color", "Image2Color","Image3Color"]
let arrayOfImageNameForUnselectedState: [String] = ["Image1NoColor","Image2NoColor","Image3NoColor"]
print(self.tabBarController?.tabBar.items?.count)
if let count = self.tabBarController?.tabBar.items?.count {
for i in 0...(count-1) {
let imageNameForSelectedState = arrayOfImageNameForSelectedState[i]
print(imageNameForSelectedState)
let imageNameForUnselectedState = arrayOfImageNameForUnselectedState[i]
print(imageNameForUnselectedState)
self.tabBarController?.tabBar.items?[i].selectedImage = UIImage(named: imageNameForSelectedState)?.withRenderingMode(.alwaysOriginal)
self.tabBarController?.tabBar.items?[i].image = UIImage(named: imageNameForUnselectedState)?.withRenderingMode(.alwaysOriginal)
}
}
在Swift 5 ioS 13.2中,TabBar样式已更改,下面的代码可以100%进行测试。
将以下代码添加到您的UITabBarController类中。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let appearance = UITabBarAppearance()
appearance.backgroundColor = .white
setTabBarItemColors(appearance.stackedLayoutAppearance)
setTabBarItemColors(appearance.inlineLayoutAppearance)
setTabBarItemColors(appearance.compactInlineLayoutAppearance)
setTabBarItemBadgeAppearance(appearance.stackedLayoutAppearance)
setTabBarItemBadgeAppearance(appearance.inlineLayoutAppearance)
setTabBarItemBadgeAppearance(appearance.compactInlineLayoutAppearance)
tabBar.standardAppearance = appearance
}
@available(iOS 13.0, *)
private func setTabBarItemColors(_ itemAppearance: UITabBarItemAppearance) {
itemAppearance.normal.iconColor = .lightGray
itemAppearance.normal.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.gray]
itemAppearance.selected.iconColor = .white
itemAppearance.selected.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange]
}
@available(iOS 13.0, *)
private func setTabBarItemBadgeAppearance(_ itemAppearance: UITabBarItemAppearance) {
//Adjust the badge position as well as set its color
itemAppearance.normal.badgeBackgroundColor = .orange
itemAppearance.normal.badgeTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
itemAppearance.normal.badgePositionAdjustment = UIOffset(horizontal: 1, vertical: -1)
}
迅捷5.3
let vc = UIViewController()
vc.tabBarItem.title = "sample"
vc.tabBarItem.image = UIImage(imageLiteralResourceName: "image.png").withRenderingMode(.alwaysOriginal)
vc.tabBarItem.selectedImage = UIImage(imageLiteralResourceName: "image.png").withRenderingMode(.alwaysOriginal)
// for text displayed below the tabBar item
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .selected)
只需向项目添加一个新的UITabBarController引用,然后在此控制器中创建UITabBar的引用:
@IBOutlet weak var appTabBar: UITabBar!
在其viewDidLoad()中,只需在标题文本颜色下面添加以下内容:
appTabBar.tintColor = UIColor.scandidThemeColor()
对于图像
tabBarItem = UITabBarItem(title: "FirstTab", image: UIImage(named: "firstImage"), selectedImage: UIImage(named: "firstSelectedImage"))
子类化TabbarViewController,并在ViewDidLoad中放置以下代码:
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor darkGreyColorBT]} forState:UIControlStateNormal];
[UITabBarItem.appearance setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor nightyDarkColorBT]} forState:UIControlStateSelected];
self.tabBar.items[0].image = [[UIImage imageNamed:@"ic-pack off@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBar.items[0].selectedImage = [[UIImage imageNamed:@"ic-pack@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBar.items[1].image = [[UIImage imageNamed:@"ic-sleeptracker off@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBar.items[1].selectedImage = [[UIImage imageNamed:@"ic-sleeptracker@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBar.items[2].image = [[UIImage imageNamed:@"ic-profile off@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBar.items[2].selectedImage = [[UIImage imageNamed:@"ic-profile@3x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
这是我拥有的最简单的工作解决方案