简体   繁体   中英

how to change UITabbar selected color?

according to this post for now, Is apple will also reject this code?

and how to implement what apple will approve?

@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

Yes , Apple will reject an app if you use that code.

I just had an app rejected for using private API calls. Specifically "_updateView". And I used the exact same code as above.

(If other people say that their app got approved with the same code it's just because it wasn't checked for use of private APIs.)

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

I suggest instead of changing color why don't you use selected tabbaritem image, like In iOS 6 I have change the selected tabbatitem image like -

in tabbar controller delegate method

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

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

through this you can change your image.

Or you can use directly in your view controllers init(or ViewWillAppear) method, like

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

for iOS 10 (or higher):

To set selected color just set:

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

Above will work for all iOS version currently supported, but to change unselected color:

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

Above code will look like this on iOS 10.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM