我正在使用 UITabBarController,
我选择 UITabBarController 选项卡它工作正常(视图控制器和选项卡图像已更改)。
但是当我尝试使用代码时
AppDelegate.Tabctrl.selectedIndex = 2;
选择了右视图控制器,但第 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;
像这样输出