第一步: 首先和使用系统自带的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];
}
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;
- (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];
// }
}
#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 必须是 0 、1、 2、 3 这个貌似到数组里取控制器,设置别的tag 没反应。
有的可能没用上 有的可能用上了,看自己需求,若发现问题 请及时评论,谢谢,如果有更好的方法请评论。本人菜鸟一枚
本文详细介绍了如何在iOS应用中自定义TabBar,包括创建自定义TabBarView,实现TabBarViewController逻辑,以及如何在AppDelegate中设置TabBar的显示与隐藏。文章还提供了关键代码片段和步骤说明,帮助开发者实现个性化TabBar功能。

被折叠的 条评论
为什么被折叠?



