iOS 自定义UITabBarView使用方法(欢迎提建议和补充完善)

本文详细介绍了如何在iOS应用中自定义TabBar,包括创建自定义TabBarView,实现TabBarViewController逻辑,以及如何在AppDelegate中设置TabBar的显示与隐藏。文章还提供了关键代码片段和步骤说明,帮助开发者实现个性化TabBar功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一步: 首先和使用系统自带的tabbar控件一样,先创建一个基于 : UITabBarController的类


HiddenTabBarViewController.h

@interface HiddenTabBarViewController : UITabBarController

{

}

- (void)setSelectedTabIndex:(NSUInteger)selectedIndex;


HiddenTabBarViewController.m


- (void)viewDidLoad

{

    [super viewDidLoad];


    //先移出系统自带的tabbarview,方便使用自己定制的

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

    {

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

        {

            view.alpha = 0;

            break;

        }

    }

}


- (void)setSelectedTabIndex:(NSUInteger)selectedIndex

{


//   切换标签时的动画 

      CATransition *transition = [CATransition animation];

//    transition.duration = 0.3f;

//    transition.type = kCATransitionFade;

//    transition.subtype = kCATransitionFromTop;

//    UIWindow *window = ((AppDelegate*)[AppDelegate GetAppDelegate]).window;

//    [window.layer addAnimation:transition forKey:nil];

    [self setSelectedIndex:selectedIndex];

}



第二步:定制自己的tabbarview,使用xib 






然后相关联:


HiddenTabBarView.h



:


@interface ITTHiddenTabBarView : UIView

{

    HiddenTabBarViewController *_tabBarController;

}

@property (nonatomic,assign) HiddenTabBarViewController *tabBarController;


@property (retain, nonatomic) IBOutlet UIButton *tab1Btn;

@property (retain, nonatomic) IBOutlet UIButton *tab2Btn;

@property (retain, nonatomic) IBOutlet UIButton *tab3Btn;

@property (retain, nonatomic) IBOutlet UIButton *tab4Btn;

@property (retain, nonatomic) IBOutlet UIButton *tab5Btn;

- (IBAction)onTabBtnClicked:(UIButton *)sender;


- (void)selectTabAtIndex:(NSInteger)index;



HiddenTabBarView.m

- (void)awakeFromNib{

    [super awakeFromNib];

}


- (void)selectTabAtIndex:(NSInteger)index

{

    _tab1Btn.selected = NO;

    _tab2Btn.selected = NO;

    _tab3Btn.selected = NO;

    _tab4Btn.selected = NO;

    _tab5Btn.selected = NO;

    

    switch (index) {

        case 0:{

            

            _tab1Btn.selected = YES;

            break;

        }

        case 1:{

            

            _tab2Btn.selected = YES;

            break;

        }

        case 2:{

            

            _tab3Btn.selected = YES;

            break;

        }

        case 3:{

            _tab4Btn.selected = YES;

            break;

        }

        case 4:{

            _tab5Btn.selected = YES;

            break;

        }

        default:

            break;

    }

}



- (IBAction)onTabBtnClicked:(UIButton *)sender {

    UIButton *bth = sender;

    [self selectTabAtIndex:bth.tag];

//    if (_tabBarController && [_tabBarController respondsToSelector:@selector(setSelectedTabIndex:)]) {

//       

        [_tabBarController setSelectedTabIndex:bth.tag];

//        [self.superview bringSubviewToFront:self];

//    }

    

}



最后:

AppDelegate.h

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;

+ (AppDelegate*)GetAppDelegate;

+ (void)HideTabBar;

+ (void)DisplayTabBar;




AppDelegate.m 

/**

 *  自定义标签栏tabbar

 */

- (void)setUpTabbarController

{

    

    _tabbarController = [[HiddenTabBarViewController alloc] init];

    NSMutableArray *navControllers = [NSMutableArray array];

    [self addViewController:[HHViewController class] toArray:navControllers];

    [self addViewController:[HHViewController class] toArray:navControllers];

    [self addViewController:[HHViewController class] toArray:navControllers];

    [self addViewController:[HHViewController class] toArray:navControllers];

    [self addViewController:[HHViewController class] toArray:navControllers];

    

    _tabbarController.viewControllers = navControllers;

    

    _customTabBarView = [[ITTHiddenTabBarView loadFromXib] retain];

    _customTabBarView.top = SCREEN_HEIGHT - _customTabBarView.height;

    _customTabBarView.tabBarController = _tabbarController;

    [_tabbarController.view addSubview:_customTabBarView];

    self.window.rootViewController = _tabbarController;

    [self customTabBarViewselectTabAtIndex:2];

    

}

- (void)addViewController:(Class)ViewController toArray:(NSMutableArray*)array{

    UIViewController *vc = [[ViewController alloc] init];

    

    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];

    nvc.navigationBar.hidden = YES;

    [vc release];

    [array addObject:nvc];

    [nvc release];

}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   

    // Override point for customization after application launch.

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];


    [self setUpTabbarController];

   

    return YES;

}


补充:

(1)自定义TabBarView 用于显示:高度:56 宽度:320

每个itemButton :64, 46 。关联xib放入合适的按钮和uiimageview

(2)定义一个tabbarViewController继承系统的tabarViewCon做一些逻辑处理。

(3) UITabBarController里面有个viewControllers属性,给该属性赋值一个数组,该数组装的是

_tabbarController = [[HiddenTabBarViewController alloc] init];

    NSMutableArray *navControllers = [NSMutableArray array];

[self addViewController:[HHLoginViewController class] toArray:navControllers];

_tabbarController.viewControllers = navControllers;

{

- (void)addViewController:(Class)ViewController toArray:(NSMutableArray*)array{

    UIViewController *vc = [[ViewController alloc] init];

    

    UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc];

    nvc.navigationBar.hidden = YES;

    [vc release];

    [array addObject:nvc];

    [nvc release];

}



}

(4)我们可以在appdelegate设置两个方法用于控制自定义tabbarview的显示和隐藏,只要改变它们的高度即可,设置动画效果会好看些。 

(5)将view加到继承自uitabbarcontroller的.view属性上面[_tabbarController.view addSubview:_customTabBarView];

self.window.rootViewController = _tabbarController;

然后通过自定义view里的设置按钮选中方法设置相关属性,为了保证显示可将自定义tabbarview 放到viewtabarControl最上面: [_customTabBarView.superview bringSubviewToFront:_customTabBarView];

需要注意一下:[self setSelectedIndex:selectedIndex]  你自定义tabbarview上写的 tag 必须是1  2 3 这个貌似到数组里取控制器,设置别的tag 没反应。 




有的可能没用上 有的可能用上了,看自己需求,若发现问题 请及时评论,谢谢,如果有更好的方法请评论。本人菜鸟一枚


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值