簡體   English   中英

更改選項卡欄項目顏色iOS7?

[英]Change Tab Bar Item Colour iOS7?

好的,我知道已經有關於此的線程,但是似乎沒有一個對我有用,對於我的特殊情況也是如此。

我有一個選項卡欄5個項目,並且我希望中心項目(索引2)在取消選擇時具有不同的顏色。 我不在乎文本是否是原始顏色,也不必關心選擇的顏色。

這是轉折點; 我正在使用“圖像”資產來為標簽欄提供圖標,如果我不使用資產,似乎所有標簽欄圖標都已像素化。 另外,在創建標簽欄控制器時,它說使用圖像資產。

誰能給我一些啟示? 謝謝。

我最終要做的是:創建了一個自定義UITabBarItem類。

@interface MyTabBarItem : UITabBarItem

- (void)setColor:(UIColor *)color forState:(UIControlState)state;

@end

它的實現:

- (void)setColor:(UIColor *)color forState:(UIControlState)state {
    NSMutableDictionary *attributes = [[self titleTextAttributesForState:state] mutableCopy];
    if (!attributes) {
        attributes = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    attributes[NSForegroundColorAttributeName] = color;
    [self setTitleTextAttributes:attributes forState:state];

    UIImage *image = [[self tintImage:self.image WithColor:color] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    if (state == UIControlStateSelected) {
        self.selectedImage = image;
    } else {
        self.image = image;
    }
}

- (UIImage *)tintImage:(UIImage *)image WithColor:(UIColor *)tintColor {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
    CGRect drawRect = CGRectMake(0, 0, image.size.width, image.size.height);
    [image drawInRect:drawRect];
    [tintColor set];
    UIRectFillUsingBlendMode(drawRect, kCGBlendModeSourceAtop);
    UIImage *tintedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return tintedImage;
}

因此,每個按鈕都可以獨立設置。 每個按鈕都使用setColor:forState:UIControlStateNormal和setColor:forState:UIControlStateSelected設置

和瞧! (適用於iOS 7分鍾)

把它放在ViewController的viewDidLoad

[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor colorWithRed:1 green:1 blue:1 alpha:1]
                                                } forState:UIControlStateNormal];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM