[英]How can I set 2 different color in Tab Bar tint color?
我有一個帶有圖像和標題的標簽欄。 如何將選定的選項卡欄項目圖像設置為漸變顏色?
當前
預期結果和原始選擇的圖像
我到目前為止所做的是:
- (void)viewDidLoad {
[super viewDidLoad];
[[UITabBar appearance] setTintColor:ThemeBlueColor];
}
是否有任何方法可以為[[UITabBar appearance] setTintColor:ThemeBlueColor];
設置漸變顏色[[UITabBar appearance] setTintColor:ThemeBlueColor];
? 請幫忙。 謝謝。
我確實更改了選項卡按鈕屬性(如標題顏色)以及使用目標C代碼選擇和取消選擇的圖像,並使用Swiftify *提供了轉換后的代碼 。
在您的第一個ViewController的tabBarController的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];
}
您以漸變色獲取圖像資產,對於選定的圖像,只需分別設置該圖像或未選擇的圖像即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.