Corona中文站

强大、易学的跨平台(iOS/Android)开发框架。QQ群1:74390406(满) 群2:221929599

导航

iPhone中如何自定义tabbar
界面看起来还比较粗糙,这里只是说明一下原理,基本思路是这样的:1. 创建一个RootViewController,它作为delegate加载的第一个controller
2. RootViewController的上半部分加载TabbarController,下半部分是自己画的控件,它用来控制Tabbar Controller加载哪个controller的
3. 把Tabbar controller的Tabbar给hide掉
4. RootViewController的下半部分是自己画的Tabbar,想怎么画就怎么画
大家看我的截图,上面是tabbar controller,它的tabbar已经给隐藏了。
下面是我自己画的自定义tabbar,还很粗糙,最左边的按钮已经可以控制tabbar的切换了。




如何隐藏tabbar控件:
首先要customize UITabBarController,其头文件定义为:

#import


@interface CustomTabbar : UITabBarController {


IBOutlet UITabBar *tabBar1;

}

@property(nonatomic, retain) UITabBar *tabBar1;

-(void) hideExistingTabBar;

@end

在实现文件中加入:

- (void)hideExistingTabBar

{


for(UIView *view in self.view.subviews)


{


if([view isKindOfClass:[UITabBar
class]])


{


view.hidden = YES;


break;


}


}

}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

[super
viewDidLoad];


[self
hideExistingTabBar];


让application delegate默认启动RootViewController

@interface MyTabbarAppDelegate : NSObject {


UIWindow *window;

RootViewController *rootController;

}

@property (nonatomic, retain) IBOutlet
UIWindow *window;

@property (nonatomic, retain) IBOutlet
RootViewController *rootController;

实现文件代码:

@implementation MyTabbarAppDelegate

@synthesize window;

@synthesize rootController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {


// Add the tab bar controller's current view as a subview of the window

[window
addSubview:rootController.view];

}

RootViewController如何加载tabbar controller:

@class
CustomTabbar;

@interface RootViewController : UIViewController {


CustomTabbar *tabbarController;

}

@property (nonatomic, retain) IBOutlet
CustomTabbar *tabbarController;

- (IBAction) clikcOnFirst:(id)sender;

@end
实现代码:

- (void)viewDidLoad {


[tabbarController.view setFrame:CGRectMake(0, 0, 300, 300)];


[self.view insertSubview:tabbarController.view atIndex:0];

[super
viewDidLoad];

}

RootViewController中的其他控件如何控制tabbar controller的切换:

- (IBAction) clikcOnFirst:(id)sender{


static
int i = 0;


tabbarController.selectedViewController = [ [tabbarController viewControllers] objectAtIndex: i];


++i;


if ( i > 6 )


i = 0;

}
<< iPhone自定义导航栏按钮开发支持iPhone横屏的Tab Bar程序 >>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最近发表

Powered By Z-Blog 1.8 Walle Build 100427 Copyright 2011-2015 BuildApp.Net. All Rights Reserved.