好岸园IT技术学习网 hopean.com IT新闻 js网页特效 it技术 二次开发

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 <UIKit/UIKit.h>



@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 <UIApplicationDelegate, UITabBarControllerDelegate> {

    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;

}

原文链接:http://www.devdiv.com/iPhone中如何自定义tabbar-weblog-1-1994.html

posted @ 2013-04-01 10:33  hopeanCom  阅读(215)  评论(0编辑  收藏  举报