我自定义UITabbar的方法在IOS 5和IOS 6中运行良好,但是在IOS7中,Tabbar不显示任何图像。
IOS6结果:
IOS7结果:
经过研究后,我尝试修复现有代码,但未成功。这是我的代码,在ios6中运行正常
#import <Foundation/Foundation.h>
@interface CustomTabBarItem : UITabBarItem
{
UIImage *selectedImg;
UIImage *unSelectedImg;
}
@property (nonatomic, retain) UIImage *selectedImg;
@property (nonatomic, retain) UIImage *unSelectedImg;
@end
#import "CustomTabBarItem.h"
@implementation CustomTabBarItem
@synthesize selectedImg;
@synthesize unSelectedImg;
-(UIImage *) selectedImage
{
return self.selectedImg;
}
-(UIImage *) unselectedImage
{
return self.unSelectedImg;
}
@end
现在在appDelegate中
self.tabBarController.delegate = self;
self.tabBarController.tabBar.frame = CGRectMake(0, self.tabBarController.tabBar.frame.origin.y, self.tabBarController.tabBar.frame.size.width, 49);
for(int i=1;i<=4;i++)
{
CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];
tabItem.selectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]];
tabItem.unSelectedImg=[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];
UIEdgeInsets titleInsets = UIEdgeInsetsMake(6.0, 0.0, -6.0, 0.0);
tabItem.imageInsets = titleInsets;
[[self.tabBarController.viewControllers objectAtIndex:i-1] setTabBarItem:tabItem];
[tabItem release];
}
上面的代码在IOS6中运行良好,在进行了一些研究之后,我对IOS7进行了一些更改
[[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
CustomTabBarItem *tabItem = [[CustomTabBarItem alloc] initWithTitle:@"" image:nil tag:0];
tabItem.image = [[UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_over_%@.png",i,deviceType]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
tabItem.selectedImage = [UIImage imageNamed:[NSString stringWithFormat:@"tab_bar-%d_%@.png",i,deviceType]];
但结果仍然相同,感谢您的任何帮助。
最佳答案
我在https://stackoverflow.com/a/20007782/1755055上看到了我的答案。
我相信在ios7中为此使用外观类属性存在限制或错误。
您的选项卡栏项使用图标图像作为模板,并使用淡色为其着色。 Apple真正希望您做的是设计用于标签栏的图标,这些图标通常是透明的,以便可以将它们用作模板图像。
有关设计这些按钮的讨论,请参见第204页的MobileHIG文档中的条形按钮图标。
因此,要设置选定的选项卡栏项目,您需要在“UITabBarItem”上调用“setSelectedImage:”,您可以从UIViewContoller获得该选项。如果UIViewController的子类由选项卡上的NavigationController包裹,则可以从该ViewController获得选项卡栏项。
我使用情节提要,因此可以在Interface Builder中设置选项卡图像。 selectedImage属性现在不存在,因此您必须在代码中进行设置。我在出现在每个选项卡中导航控制器堆栈顶部的每个主视图控制器中都执行了此操作。
您的示例需要按设计渲染图像,因此还需要在图像上设置渲染模式。
- (void)viewDidLoad
{
[super viewDidLoad];
...
[self.navigationController.tabBarItem setSelectedImage:[[UIImage imageNamed:@"MySelectedIcon.png"]
imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
}
关于ios - IOS 7中的自定义UITabbar问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19965400/