ios - 如何在标签栏色调颜色中设置 2 种不同的颜色?
问题描述
我有一个带有图像和标题的标签栏。如何将选定的标签栏项目图像设置为渐变色?
当前的
预期结果和原始选择图像
到目前为止我做了什么: -
- (void)viewDidLoad {
[super viewDidLoad];
[[UITabBar appearance] setTintColor:ThemeBlueColor];
}
有什么方法可以设置渐变色[[UITabBar appearance] setTintColor:ThemeBlueColor];
吗?请帮忙。谢谢你。
解决方案
我确实已经更改了标签栏按钮属性,例如标题颜色以及使用目标 c 代码选择和取消选择的图像,并提供了* 使用 Swiftify* 转换的代码。
在 tabBarController 的第一个 ViewController 的 viewDidLoad() 中调用这个方法,你就可以开始了
请看一看 - :
// Converted with Swiftify v1.0.6472 - https://objectivec2swift.com/
func setTabBarSelectedDeselectedIconsAndTitleColor() {
let recentItem = tabBarController?.tabBar.items[0] as? UITabBarItem
recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
recentItem?.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
recentItem?.image = UIImage(named: "home_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
recentItem?.selectedImage = UIImage(named: "home_select_icon.png")?.withRenderingMode(.alwaysOriginal)
recentItem = tabBarController?.tabBar.items[1]
recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .normal)
recentItem.setTitleTextAttributes([.foregroundColor: UIColor(red: 91.0 / 255.0, green: 46.0 / 255.0, blue: 224.0 / 255.0, alpha: 1.0)], for: .selected)
recentItem.image = UIImage(named: "kid_location_unselect_icon.png")?.withRenderingMode(.alwaysOriginal)
recentItem.selectedImage = UIImage(named: "kid_location_select_icon.png")?.withRenderingMode(.alwaysOriginal)
}
类似的objective-c等价物-:
-(void)setTabBarSelectedDeselectedIconsAndTitleColor{
UITabBarItem *recentItem = self.tabBarController.tabBar.items[0];
[recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateNormal];
[recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateSelected];
recentItem.image = [[UIImage imageNamed:@"home_unselect_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
recentItem.selectedImage = [[UIImage imageNamed:@"home_select_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
recentItem = self.tabBarController.tabBar.items[1];
[recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateNormal];
[recentItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:91.0f/255.0f green:46.0f/255.0f blue:224.0f/255.0f alpha:1.0f]} forState:UIControlStateSelected];
recentItem.image = [[UIImage imageNamed:@"kid_location_unselect_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
recentItem.selectedImage = [[UIImage imageNamed:@"kid_location_select_icon.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
推荐阅读
- typescript - Angular 6 依赖服务调用
- smtp - SMTP 错误:无法连接到服务器:网络无法访问 (101) SMTP 连接失败
- javascript - 使用Break进行循环无限循环?反应式
- wcf - WCF - 在 HTTPS 情况下未使用 HTTP.SYS 正确配置证书。与 Charles 代理一起运行
- javascript - Json 解码“试图获取非对象的属性”错误
- java - 如何修复 java.lang.SecurityException:调用者缺少所需的权限 com.google.android.things.permission.USE_PERIPHERAL_IO 错误?
- php - 如果变量为空,则使用另一个值
- vue.js - 使用 vue-cli 构建深度嵌套的 html 需要很长时间
- python - 改进代码以查找素数
- c# - C# 清除定时器中的richTextBox