自定义UITabBar(原创) – 苏瑞强’Blog – 博客园.

@interface CustomerTabBarController : UITabBarController {
}
@end

@implementation CustomerTabBarController
//@synthesize tabBarTest;

– (void)viewDidLoad {

//方法一,使用颜色自定义UItabBar :
/*
[super viewDidLoad];
CGRect frame = CGRectMake(0, 0, 320, 44);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIColor *c = [[UIColor alloc] initWithRed:0.4 green:0.7 blue:0.3 alpha:1.0];
v.backgroundColor = c;
[c release];
[self.tabBar insertSubview:v atIndex:0];
[v release];
NSLog(@”view did load”);
*/

//方法二,使用图片自定义UItabBar :
[super viewDidLoad];
UIImageView* image = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:PACKAGE_FILE_PATH(@”tabbar.png”)]];
//image.frame = CGRectOffset(image.frame, 0, 5);
image.frame = CGRectMake(0, 0, 320,49);
[self.tabBar insertSubview:image atIndex:0];

  • ;
    }
    @end

    调用的地方:

    在*delegate.m众调用:

    -(void)setupViewControllers
    {
    UINavigationController* localNavCtroller;
    NSMutableArray* localViewCtrlArray =  [[NSMutableArray alloc] init];

    localNavCtroller = [self createNavControllerWrappingView:[HomeViewController class] tabIconName:nil tabTitle:nil];
    [localViewCtrlArray addObject:localNavCtroller];
    [localNavCtroller release];

    localNavCtroller = [self createNavControllerWrappingView:[StoredDataViewController class] tabIconName:nil tabTitle:nil];
    [localViewCtrlArray addObject:localNavCtroller];
    [localNavCtroller release];

    localNavCtroller = [self createNavControllerWrappingView:nil tabIconName:nil tabTitle:nil];
    [localViewCtrlArray addObject:localNavCtroller];
    [localNavCtroller release];
    tabBarController.viewControllers = localViewCtrlArray;
    [localViewCtrlArray release];
    }

    -(UINavigationController *)createNavControllerWrappingView:(Class)controller
    tabIconName:(NSString*)iconName
    tabTitle:(NSString*)tabTitle
    {
    UIViewController* viewCtrller = [[controller alloc] init];
    UINavigationController* theNavCtrller = [[UINavigationController alloc] initWithRootViewController:viewCtrller];
    theNavCtrller.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    viewCtrller.tabBarItem.image = [UIImage imageNamed:iconName];
    viewCtrller.title = NSLocalizedString(tabTitle, @””);
    [viewCtrller release];
    return theNavCtrller;
    }
    <p class=”p1″>- (<span class=”s1″>void</span>)applicationDidFinishLaunching:(<span class=”s2″>UIApplication</span> *)application&nbsp;</p>{<p class=”p1″><span class=”s1″>window</span><span class=”s2″> = [[</span>UIWindow<span class=”s2″> </span>alloc<span class=”s2″>]</span>initWithFrame<span class=”s2″>:[[</span>UIScreen<span class=”s2″> </span>mainScreen<span class=”s2″>]</span>bounds<span class=”s2″>]];</span></p>
    <p class=”p1″><span class=”s2″>&nbsp; &nbsp; [</span><span class=”s1″>window</span><span class=”s2″> </span>makeKeyAndVisible<span class=”s2″>];</span></p>
    <p class=”p2″><span class=”s2″>&nbsp;</span>tabBarController<span class=”s2″> = [[</span>CustomerTabBarController<span class=”s2″> </span><span class=”s3″>alloc</span><span class=”s2″>] </span><span class=”s3″>init</span><span class=”s2″>];</span></p>
    <p class=”p2″><span class=”s2″>[</span><span class=”s4″>self</span><span class=”s2″> </span>setupViewControllers<span class=”s2″>];</span></p>
    <p class=”p2″><span class=”s2″>[</span>window<span class=”s2″> </span><span class=”s3″>addSubview</span><span class=”s2″>:</span>tabBarController<span class=”s2″>.</span><span class=”s3″>view</span><span class=”s2″>];</span></p><br>

    Leave a Reply

    电子邮件地址不会被公开。 必填项已用*标注