标签栏控制器中TabBar项目的不同颜色
我有选项卡栏控制器几个选项卡。
-
所有标签项目的图像和文字应该具有相同的非select颜色。
-
所有标签项目应该有不同的图像和文字select的颜色。
我准备创build具有选定和非select项目所需颜色的图像。 而且我知道如何更改AppDelegate中所有选项卡的颜色
UITabBar.appearance().tintColor = UIColorFromRGB(rgbValue: MY_COLOR, aplhaValue: 1.0) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColorFromRGB(rgbValue: MY_COLOR, aplhaValue: 1.0)], for: UIControlState.selected) UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.white], for: UIControlState.normal)
但是我想为不同的项目制作不同的颜色。
在你的Storyboard中select你的TabBarController并给它一个自定义类:在这个例子中, TabBarController
:
然后创build自定义的TabBarController文件:
// Copyright © 2017 Erick Vavretchek. All rights reserved. import UIKit class TabBarController: UITabBarController { enum tabBarMenu: Int { case home case list case settings } // MARK: UITabBarController override func viewDidLoad() { super.viewDidLoad() guard let tabBarMenuItem = tabBarMenu(rawValue: 0) else { return } setTintColor(forMenuItem: tabBarMenuItem) } override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { guard let menuItemSelected = tabBar.items?.index(of: item), let tabBarMenuItem = tabBarMenu(rawValue: menuItemSelected) else { return } setTintColor(forMenuItem: tabBarMenuItem) } // MARK: Private private func setTintColor(forMenuItem tabBarMenuItem: tabBarMenu) { switch tabBarMenuItem { case .home: viewControllers?[tabBarMenuItem.rawValue].tabBarController?.tabBar.tintColor = UIColor.green case .list: viewControllers?[tabBarMenuItem.rawValue].tabBarController?.tabBar.tintColor = UIColor.red case .settings: viewControllers?[tabBarMenuItem.rawValue].tabBarController?.tabBar.tintColor = UIColor.red } } }
在您的Assets.xcassets文件夹中,select您用于TabBarItem的每个图像并将它们设置为“ Render As: Template Image
也很重要。 这就是你可以简单地改变它的tintColor为任何你喜欢的:
- ScrollView阻止NavigationViewController
- 如何获取UIImagePickerController所选图像的原始文件名?
- openActiveSessionWithReadPermissions不会自动login
- 绕过http响应头Cache-Control:如何设置缓存过期?
- 如何确定GMSMarker是否位于GMSPolygon(iOS Google Map SDK)
- iOS – 无法使用Twitter / Fabric新SDK上传媒体
- 在UIwebview中播放本地video文件
- UIImageView内的UIButton不响应水龙头
- 是否有可能改变UILocalNotification的显示持续时间?