如何更改UITabbar选择的颜色?

时间:2010-03-24 08:44:23

标签: iphone uitabbarcontroller

根据此post 现在,苹果还会拒绝这个代码吗?

以及如何实施苹果将批准的内容?

@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

4 个答案:

答案 0 :(得分:5)

,如果您使用该代码,Apple 拒绝某个应用。

我刚刚使用私有API调用拒绝了应用。特别是“_updateView”。我使用了与上面完全相同的代码。

(如果其他人说他们的应用程序获得了相同的代码批准,那只是因为没有检查是否使用了私有API。)

答案 1 :(得分:3)

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

答案 2 :(得分:0)

我建议你不要改变颜色,为什么不使用选定的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"]];

答案 3 :(得分:0)

适用于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上看起来像这样。

enter image description here