ここでは、4 つのボタンを持つカスタム タブ バーを作成しました。ボタンは画像で構成され、定数ファイルに取り込まれました。ここで、serviceImg、contactImg などは、.h ファイルで宣言されている UIImageView です。また、UITabBarControllerDelegate をデリゲートとして .h ファイルに追加することを忘れないでください。
-(void)setUpTabBar {
tabBar = [[UITabBarController alloc] init];
Services *firstViewController = [[Services alloc]init];
firstViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
UINavigationController *firstNavController = [[UINavigationController alloc]initWithRootViewController:firstViewController];
ContactUs *secondViewController = [[ContactUs alloc]init];
secondViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:2];
UINavigationController *secondNavController = [[UINavigationController alloc]initWithRootViewController:secondViewController];
Bookings *thirdViewController = [[Bookings alloc]init];
thirdViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:3];
UINavigationController *thirdNavController = [[UINavigationController alloc]initWithRootViewController:thirdViewController];
Reward *fourthViewController = [[Reward alloc]init];
fourthViewController.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemRecents tag:4];
UINavigationController *fourthNavController = [[UINavigationController alloc]initWithRootViewController:fourthViewController];
tabBar.viewControllers = [[NSArray alloc] initWithObjects:firstNavController, secondNavController, thirdNavController, fourthNavController, nil];
tabBar.delegate=self;
tabBar.selectedIndex=0;
[firstNavController release];
[firstViewController release];
[secondNavController release];
[secondViewController release];
[thirdNavController release];
[thirdViewController release];
[fourthNavController release];
[fourthViewController release];
serivceImg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 432, 80, 49)];
serivceImg.image=[UIImage imageNamed:serviceHover];
contactImg=[[UIImageView alloc]initWithFrame:CGRectMake(81, 432,80, 49)];
contactImg.image=[UIImage imageNamed:tabContact];
bookingImg=[[UIImageView alloc]initWithFrame:CGRectMake(162, 432,80, 49)];
bookingImg.image=[UIImage imageNamed:tabBooking];
rewardImg=[[UIImageView alloc]initWithFrame:CGRectMake(243, 432, 80, 49)];
rewardImg.image=[UIImage imageNamed:tabReward];
[tabBar.view addSubview:serivceImg];
[tabBar.view addSubview:contactImg];
[tabBar.view addSubview:bookingImg];
[tabBar.view addSubview:rewardImg];
[[[UIApplication sharedApplication]keyWindow]addSubview:tabBar.view];
[serivceImg release];
[contactImg release];
[bookingImg release];
[rewardImg release];
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController1{
if (viewController1 == [tabBar.viewControllers objectAtIndex:0])
{
serivceImg.image = [UIImage imageNamed:kserviceHover];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:1])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:kcontactHover];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:2])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:kbookingHover];
rewardImg.image=[UIImage imageNamed:ktabReward];
}
else if (viewController1 == [tabBar.viewControllers objectAtIndex:3])
{
serivceImg.image = [UIImage imageNamed:ktabService];
contactImg.image=[UIImage imageNamed:ktabContact];
bookingImg.image=[UIImage imageNamed:ktabBooking];
rewardImg.image=[UIImage imageNamed:krewardHover];
}
}
これで問題が解決することを願っています。