我正在使用 UITabBarController,
我选择 UITabBarController Tab 工作正常( View Controller 和选项卡图像已更改)。
但是当我尝试使用代码
AppDelegate.Tabctrl.selectedIndex = 2;
选择了右 View Controller ,但选项卡图像位于第 0 个选项卡本身,我尝试使用 TabBarController 的手动图像。这是代码。
-(void) LoadTabbarcontrol
{
UIStoryboard *board = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
AppDelegate.Tabctrl = [board instantiateViewControllerWithIdentifier:@"TabbarCtrlId"];
AppDelegate.Tabctrl.delegate = self;
UIWindow *window = AppDelegate.window;
[window addSubview:AppDelegate.Tabctrl.view];
[window makeKeyAndVisible];
imgTab = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,320.0,44.0)];
imgTab.image=[UIImage imageNamed:@"Tabbar_bg.png"];
[AppDelegate.Tabctrl.tabBar addSubview:imgTab];
imgHome=[[UIImageView alloc]initWithFrame:CGRectMake(4.0,1.0,60.0,44.0)];
imgHome.image=[UIImage imageNamed:@"Home-Select.png"];
[imgTab addSubview:imgHome];
imgMyTc=[[UIImageView alloc]initWithFrame:CGRectMake(67.0,1.0,60.0,44.0)];
imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
[imgTab addSubview:imgMyTc];
imgBrowse=[[UIImageView alloc]initWithFrame:CGRectMake(130.0,1.0,60.0,44.0)];
imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
[imgTab addSubview:imgBrowse];
imgInstore=[[UIImageView alloc]initWithFrame:CGRectMake(193.0,1.0,60.0,44.0)];
imgInstore.image=[UIImage imageNamed:@"Instore.png"];
[imgTab addSubview:imgInstore];
imgMore=[[UIImageView alloc]initWithFrame:CGRectMake(256.0,1.0,60.0,44.0)];
imgMore.image=[UIImage imageNamed:@"More.png"];
[imgTab addSubview:imgMore];
AppDelegate.Tabctrl.view.hidden = NO;
AppDelegate.Tabctrl.selectedIndex = 2;
}
- (void)tabBarController:(UITabBarController *)tabBarControllers didSelectViewController:(UIViewController *)viewController
{
if (tabBarControllers.selectedIndex == 0)
{
imgHome.image=[UIImage imageNamed:@"Home-Select.png"];
imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
imgInstore.image=[UIImage imageNamed:@"Instore.png"];
imgMore.image=[UIImage imageNamed:@"More.png"];
}
else if (tabBarControllers.selectedIndex == 1)
{
imgHome.image=[UIImage imageNamed:@"Home.png"];
imgMyTc.image=[UIImage imageNamed:@"MyTc-Select.png"];
imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
imgInstore.image=[UIImage imageNamed:@"Instore.png"];
imgMore.image=[UIImage imageNamed:@"More.png"];
}
else if (tabBarControllers.selectedIndex == 2)
{
imgHome.image=[UIImage imageNamed:@"Home.png"];
imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
imgBrowse.image=[UIImage imageNamed:@"Browse-Select.png"];
imgInstore.image=[UIImage imageNamed:@"Instore.png"];
imgMore.image=[UIImage imageNamed:@"More.png"];
}
else if (tabBarControllers.selectedIndex == 3)
{
imgHome.image=[UIImage imageNamed:@"Home.png"];
imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
imgInstore.image=[UIImage imageNamed:@"Instore-Select.png"];
imgMore.image=[UIImage imageNamed:@"More.png"];
}
else if (tabBarControllers.selectedIndex == 4)
{
imgHome.image=[UIImage imageNamed:@"Home.png"];
imgMyTc.image=[UIImage imageNamed:@"MyTc.png"];
imgBrowse.image=[UIImage imageNamed:@"Browse.png"];
imgInstore.image=[UIImage imageNamed:@"Instore.png"];
imgMore.image=[UIImage imageNamed:@"More-Select.png"];
}
}
谁能帮我解决这个问题吗?
如果我给出,请参阅示例屏幕
AppDelegate.Tabctrl.selectedIndex = 2;
输出如下
最佳答案
尝试使用此代码更改 Tabbar Controller 的图像:
[[AppDelegate.Tabctrl.tabBar.items objectAtIndex:1] setFinishedSelectedImage:[UIImage imageNamed:@"message12_64.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"message_112_64.png"]];
关于ios - UITabBarController 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899288/