ここでは、カスタムタブバーを実装するためのチュートリアルを提供できます
Delegate.hファイル
@interface cTabBarAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
@property (nonatomic, retain) IBOutlet UIImageView *imgV;
@end
Delegate.mファイル
@implementation cTabBarAppDelegate
@synthesize window=_window;
@synthesize tabBarController=_tabBarController;
@synthesize imgV = _imgV;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.tabBarController.delegate=self;
self.imgV.frame=CGRectMake(0, 425, 320, 55);
[self.tabBarController.view addSubview:self.imgV];
self.tabBarController.selectedIndex=0;
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController];
switch (index) {
case 0:
self.imgV.image=[UIImage imageNamed:@"tBar1.png"];
break;
case 1:
self.imgV.image=[UIImage imageNamed:@"tBar2.png"];
break;
case 2:
self.imgV.image=[UIImage imageNamed:@"tBar3.png"];
break;
case 3:
self.imgV.image=[UIImage imageNamed:@"tBar4.png"];
break;
case 4:
self.imgV.image=[UIImage imageNamed:@"tBar5.png"];
break;
default:
break;
}
return YES;
}
次のコードはviewcontroller.mに入れられます
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
SearchVctr *viewController1 = [[SearchVctr alloc] initWithNibName:@"SearchVctr" bundle:nil];
UINavigationController *navForRentResidental= [[UINavigationController alloc]initWithRootViewController:viewController1];
UIViewController *viewController2 = [[FavouriteVctr_iphone alloc] initWithNibName:@"FavouriteVctr_iphone" bundle:nil];
UINavigationController *navForFavorite= [[UINavigationController alloc]initWithRootViewController:viewController2];
UIViewController *viewController3 = [[LoginAndRegisterVctr alloc] initWithNibName:@"LoginAndRegisterVctr" bundle:nil];
UINavigationController *navForLogin= [[UINavigationController alloc]initWithRootViewController:viewController3];
navForLogin.navigationBar.hidden=TRUE;
UIViewController *viewController4 = [[ContactUsVctr alloc] initWithNibName:@"ContactUsVctr" bundle:nil];
UINavigationController *navForContact= [[UINavigationController alloc]initWithRootViewController:viewController4];
UIViewController *viewController5 = [[MoreVctr_iphone alloc] initWithNibName:@"MoreVctr_iphone" bundle:nil];
UINavigationController *navForMore= [[UINavigationController alloc]initWithRootViewController:viewController5];
navForContact.navigationBar.hidden=TRUE;
navForMore.navigationBar.hidden=TRUE;
navForLogin.navigationBar.hidden=TRUE;
navForFavorite.navigationBar.hidden = TRUE;
if([sender tag] == 1){
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
delegate.tabBarController.selectedIndex = 3;
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t4.png"]];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:delegate.tabBarController animated:YES];
}else if([sender tag] == 2){
UIViewController *viewController5 = [[AboutVctr alloc] initWithNibName:@"AboutVctr" bundle:nil];
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, viewController5, nil];
delegate.tabBarController.selectedIndex = 4;
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t5.png"]];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:delegate.tabBarController animated:YES];
}
else if([sender tag] == 3){
UIViewController *viewController5 = [[ServicesVctr alloc] initWithNibName:@"ServicesVctr" bundle:nil];
UINavigationController *navcontroller= [[UINavigationController alloc]initWithRootViewController:viewController5];
navcontroller.navigationBar.hidden=TRUE;
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navcontroller, nil];
delegate.tabBarController.selectedIndex = 4;
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t5.png"]];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:delegate.tabBarController animated:YES];
}
else if([sender tag] == 4){
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
delegate.tabBarController.selectedIndex = 2;
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t3.png"]];
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t3.png"]];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:delegate.tabBarController animated:YES];
}
else if([sender tag] == 5){
delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:navForRentResidental, navForFavorite, navForLogin, navForContact, navForMore, nil];
delegate.tabBarController.selectedIndex = 0;
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t1.png"]];
delegate.imgV.image=[UIImage imageNamed:[NSString stringWithFormat:@"t1.png"]];
delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:delegate.tabBarController animated:YES];
}
このコードは開発に役立つ可能性があります。