簡體   English   中英

如何更改UITabbar選擇的顏色?

[英]how to change UITabbar selected color?

根據這篇文章 ,蘋果現在也會拒絕這個代碼嗎?

以及如何實施蘋果將批准的內容?

@interface UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur;
@end

@interface UITabBarItem (Private)
@property(retain, nonatomic) UIImage *selectedImage;
- (void)_updateView;
@end

@implementation UITabBar (ColorExtensions)
- (void)recolorItemsWithColor:(UIColor *)color shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)shadowOffset shadowBlur:(CGFloat)shadowBlur
{
        CGColorRef cgColor = [color CGColor];
        CGColorRef cgShadowColor = [shadowColor CGColor];
        for (UITabBarItem *item in [self items])
                if ([item respondsToSelector:@selector(selectedImage)] &&
                    [item respondsToSelector:@selector(setSelectedImage:)] &&
                    [item respondsToSelector:@selector(_updateView)])
                {
                        CGRect contextRect;
                        contextRect.origin.x = 0.0f;
                        contextRect.origin.y = 0.0f;
                        contextRect.size = [[item selectedImage] size];
                        // Retrieve source image and begin image context
                        UIImage *itemImage = [item image];
                        CGSize itemImageSize = [itemImage size];
                        CGPoint itemImagePosition; 
                        itemImagePosition.x = ceilf((contextRect.size.width - itemImageSize.width) / 2);
                        itemImagePosition.y = ceilf((contextRect.size.height - itemImageSize.height) / 2);
                        UIGraphicsBeginImageContext(contextRect.size);
                        CGContextRef c = UIGraphicsGetCurrentContext();
                        // Setup shadow
                        CGContextSetShadowWithColor(c, shadowOffset, shadowBlur, cgShadowColor);
                        // Setup transparency layer and clip to mask
                        CGContextBeginTransparencyLayer(c, NULL);
                        CGContextScaleCTM(c, 1.0, -1.0);
                        CGContextClipToMask(c, CGRectMake(itemImagePosition.x, -itemImagePosition.y, itemImageSize.width, -itemImageSize.height), [itemImage CGImage]);
                        // Fill and end the transparency layer
                        CGContextSetFillColorWithColor(c, cgColor);
                        contextRect.size.height = -contextRect.size.height;
                        CGContextFillRect(c, contextRect);
                        CGContextEndTransparencyLayer(c);
                        // Set selected image and end context
                        [item setSelectedImage:UIGraphicsGetImageFromCurrentImageContext()];
                        UIGraphicsEndImageContext();
                        // Update the view
                        [item _updateView];
                }
}
@end

是的 ,如果您使用該代碼,Apple 拒絕某個應用。

我剛剛使用私有API調用拒絕了應用。 特別是“_updateView”。 我使用了與上面完全相同的代碼。

(如果其他人說他們的應用程序被批准使用相同的代碼,那只是因為沒有檢查是否使用了私有API。)

[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];

我建議不要改變顏色為什么你不使用選定的tabbaritem圖像,如在iOS 6我已經改變選定的tabbatitem圖像像 -

在tabbar控制器委托方法中

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

{
    if([tabBarController selectedIndex] == 0)
    {
        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];
    }    
}

通過這個你可以改變你的形象。

或者你可以直接在你的視圖控制器init(或ViewWillAppear)方法中使用

        [viewController.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"selected.png"]withFinishedUnselectedImage:[UIImage imageNamed:@"unselect.png"]];

對於iOS 10(或更高版本):

要設置所選顏色,請設置:

let tabBarAppearace = UITabBar.appearance()
tabBarAppearace.tintColor = UIColor.nowYouBlue

以上將適用於當前支持的所有iOS版本,但更改未選擇的顏色:

    if #available(iOS 10.0, *) {
        tabBarAppearace.unselectedItemTintColor = UIColor.red
    } else {
        // Fallback on earlier versions
    }

上面的代碼在iOS 10上看起來像這樣。

在此輸入圖像描述

暫無
暫無

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

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