如何改变UITabbar选择的颜色?

发布于 2024-08-26 04:45:33 字数 3106 浏览 2 评论 0原文

根据此帖子 就目前而言,苹果是否也会拒绝这个代码?

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

@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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一向站在原地 2024-09-02 04:45:33

,如果您使用该代码,Apple拒绝应用程序。

我刚刚有一个应用程序因使用私有 API 调用而被拒绝。具体来说是“_updateView”。我使用了与上面完全相同的代码。

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

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.)

相思碎 2024-09-02 04:45:33
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];
爱她像谁 2024-09-02 04:45:33

我建议您为什么不使用选定的 tabbaritem 图像而不是更改颜色,
喜欢
在 iOS 6 中,我已经更改了选定的 tabbatitem 图像,例如 -

在选项卡控制器委托方法中,

- (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"]];

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"]];
空‖城人不在 2024-09-02 04:45:33

对于 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 上将如下所示。

在此处输入图像描述

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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文