kin*_*ton 7 xcode objective-c uitabbarcontroller uitabbaritem
我有四个标签.我能够将标签图标颜色从默认的蓝色更改为红色(或者可能是任何颜色),并且它可以完美地运行.问题是它只适用于三个tabbaritems,最后一个是默认蓝色.下面是代码.我正在编写此rootviewcontrollerAppDelegate.m代码您可以通过在appdelegate中粘贴以下代码来尝试此操作.你们能帮助我吗?我会非常感激!
@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
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
[[tabBarController tabBar] recolorItemsWithColor:[UIColor redColor] shadowColor:[UIColor blackColor] shadowOffset:CGSizeMake(0.0f, -1.0f) shadowBlur:3.0f];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
[self addTabBarArrow];
return YES;
}
Run Code Online (Sandbox Code Playgroud)

[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
小智 2
自添加 tabbar-item 没问题,我测试了 4 个项目的代码;
但您的最后一个选项卡栏项目是系统选项卡栏项目(“....”“更多”项目),因此此代码可能没有用处;它只是不使用您设置的图像;
| 归档时间: |
|
| 查看次数: |
6883 次 |
| 最近记录: |