我是IOS开发的新手。我在为 Tabbar 项目动态设置图像时遇到问题。我对 UITabbarItem 进行了分类。
@interface CustomTabBarItem : UITabBarItem
{
UIImage *customHighlightedImage;
UIImage *customStdImage;
}
@property (nonatomic, retain) UIImage *customHighlightedImage;
@property (nonatomic, retain) UIImage *customStdImage;
@end
并将我的 tabbarcontroller 添加到窗口中。
[_tabBarController setViewControllers: [[NSArray alloc] initWithObjects:1,2,3,4,nil] animated: YES];
_tabBarController.selectedIndex = 0;
_tabBarController.delegate = self;
_tabBarController.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"tabbarBackground.png"]];
[self.window addSubview:_tabBarController.view];
self.window.rootViewController = _tabBarController;
[self.window makeKeyAndVisible];
我的 Tabbar 控制器完美支持所有方向。但问题是当我旋转设备时,如何为设备的当前方向设置标签栏项目的图像。框架会自动为每个选项卡设置,但我的图像没有。我的应用程序中有四个选项卡。我通过该网站进行了很多搜索,但无法获得适当的解决方案。
编辑
这是我用于设置图像的代码。
tabBarItemBrowse=[[CustomTabBarItem alloc] init];
tabBarItemBrowse.imageInsets=UIEdgeInsetsMake(6, 0, -6, 0);
// tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser.png"];
// tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act.png"];
if ( [UIApplication sharedApplication].statusBarOrientation==UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser.png"];
tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act.png"];
}
else
{
tabBarItemBrowse.customStdImage=[UIImage imageNamed:@"browser568.png"];
tabBarItemBrowse.customHighlightedImage=[UIImage imageNamed:@"browser_act568.png"];
}
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
BrowseNav = [[CustomNavigationControllerViewController alloc] initWithRootViewController:self.viewController];
BrowseNav.tabBarItem = tabBarItemBrowse;
BrowseNav.navigationBar.tintColor = [UIColor blackColor];
BrowseNav.navigationBarHidden=YES;
[tabBarItemBrowse release];