导航控制器,自定义一个形状不规则的TabBar

2020-03-31 20:47 来源:未知

效果图如下:

概念

  • UINavigationController 继承于 UIViewController
  • 包含:viewcontrollers、NavigationBar、Toolbar
  • 导航控制器是一个堆栈结构,只是其中管理的对象是controller,通过push与pop进行controller的切换,UINavigationController是将这些控件(UINavigationBar,UINavigationItem和UIToolBar)和UIViewController紧密的结合了起来

在使用TabBarController的过程中,经常需要让TabBar的item点击事件能加上动画,或者item需要用较大的图片,超出了TabBar的高度与最大的图片像素限制。这种情况下,在TabBarController中,使用默认的TabBar比较难实现,超出TabBar高度的区域不会响应点击事件,色彩过于丰富的图片甚至无法正常显示。自定义TabBar如下两个APP:

无NavigationBar到有NavigationBar视图切换时的一个坑,navigationbar视图

  NavigationController在iOS App中是最常见不过了,可以说是每个App中必备的了。自iOS7开始,系统自带的右滑返回效果,也可以让有NavigationBar的视图切换很丝滑流畅。当然你也可能会遇到有些视图并没有NavigationBar(实际上是隐藏了),然后从这样的视图中push一个有NavigationBar的视图,这种需求很常见(姑且不讨论这种设计到底合不合适),实现起来也很简单,就是在不同的视图里对NavigationBar设置隐藏和显示就可以了,代码如下:

1 - (void)viewWillAppear:(BOOL)animated {
2 
3     [super viewWillAppear:animated];
4 
5     self.navigationController.navigationBarHidden = YES;
6 
7 }

  这里需要注意一点,因为设置NavigationBar的隐藏或显示是作用到当前navigationcontroller的,他会影响到整个navigationcontroller的NavigationBar的显示情况,所以一定要放到viewWillAppear里,不能放到viewDidLoad里。
  看下效果:

  图片 1

  

  嗯,好像还不错。等等,pop的时候好像右上角有个黑色的东东,那是什么鬼?用iOS7系统自带的右滑返回功能仔细看看(手指按住不松开可以慢慢移动看看效果):

  图片 2

  

  哦,shit!告诉我这不是真的!但这就是真的!接受现实吧,骚年!
  心中一万只草泥马奔腾过后,开始寻找解决办法。
  开始以为是NavigationBar透明度的问题,试了好几个方案没有解决。
  最后无意中找到解决办法,然后差点哭晕在厕所,代码如下:

1 - (void)viewWillAppear:(BOOL)animated {
2     [super viewWillAppear:animated];
3 
4 //    self.navigationController.navigationBarHidden = YES;
5     [self.navigationController setNavigationBarHidden:YES animated:YES];
6 }

  再看下现在的效果:

图片 3

嗯,不错!很有丝滑的感觉,嘿嘿!

总结一下,问题的关键在于那个animated,必须是YES才能达到理想的效果,如果换成下面的代码[self.navigationController setNavigationBarHidden:YES animated:NO],依然会有黑色底。定位到这个方法里看到这么一句话:
If animated, it will transition vertically using UINavigationControllerHideShowBarDuration.
大致意思是如果animated=YES,会使用UINavigationControllerHideShowBarDuration进行垂直过渡。UINavigationControllerHideShowBarDuration是一个CGFloat类型的常量。
可是我并没有很好的理解它所谓的transition vertically到底是个什么概念,也许就是后来看到的那个效果吧。。。

(文笔不好,如有错误,欢迎指正,不甚感激)

NavigationController在iOS App中是最常见不过了,可以说是每个App中必备的了。自...

图片 4仁伯安.gif

总结:

图片 5飞猪首页TabBar图片 6inke TabBar.gif

左滑重点不在动画效果的实现,而在于左边视图事件响应后,怎么push进入主视图的NavigationController,如果主视图是TabBar的话,要获取当前TabBar的selecteItem,并push进入对应的导航容器中,正文以最为典型的TabBar作为Main主视图。

NavigationController管理的东西:
  1. NavigationController管理多个Controller
  2. NavigationController管理着NavigationBar(通过navigationController. navigationBar方式可以调用)
    NavigationBar管理多个NavigationItem,和NavigationController一样都是用push和pop的方式推进推出各自管理的东西NavigationItem包含多个UIBarButtonItem
  3. NavigationController管理着ToolBar,通过navigationController.toolbar方式调用

对于这样的TabBar,我们往往需要自定义。本文仿映客iOS客户端的自定义TabBar思路是先用UIView来绘制所需要的样式,用Button代替其上的item,将绘制好的View作为subView添加到self上(self是一个TabBar对象)。定义delegate来处理对应按钮的点击事件。为了解决中间按钮部分覆盖了左右两个Button,并且高出底下View高度,导致形状不规则使点击事件无法正确响应的问题,重写了-hitTest:point withEvent:(UIEvent *)event方法,最后,用KVC的方式替换了TabBarController中的默认TabBar。

1、首先实现工具类EVNSideSlideViewController,支持左滑和右滑,左视图-主视图-右视图,记得用的时候要将EVNSideSlideViewController做为你的根视图;2、左右视图的设计及主视图控制器的设计,按照原来做;3、使用单例类记录主视图TabBar的选择状态,以便于push到当前页面所在的NavigationController中;4、左右视图单击事件处理,详情见,正文的第三部分。

控制器管理的东西

每个界面的Controller管理一个它界面的NavigationItem(通过controller. navigationItem方式可以调用)


首先,创建一个基于UITabBar的子类SamTabBar。为什么基于UITabBar而不是UIView?因为TabBarController和NavigationController结合时,当我们想push到一个childVC时,在其中调用[self.navigationController pushViewController:childVC animated:YES];,可以使用系统自带的属性childVC.hidesBottomBarWhenPushed = YES;让TabBar在push时自动隐藏,而不用自己添加一些动画代码来完成这一效果。

下面的BaseViewController可以改成UIViewController

属性

1、获取栈顶的controller

@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController;

2、获取当前界面显示的controller

@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;

3、 给导航控制器设置controller

@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;

例子: //将上面的这5个控制器添加到控制器数组中

self.viewControllers = @[navHall, navArena, navDiscovery, navHistory, navMyLottery];

4、设置隐藏导航栏

@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;

5、 获取导航栏

@property(nonatomic,readonly) UINavigationBar *navigationBar;

6、是否隐藏底部工具条(默认隐藏)

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden;

7、获取底部工具条

@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar;

8、获取导航中的返回手势对象(iOS7之后,在导航中右划会进行pop操作,设置这个的enable可以控制设置手势是否失效)

@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer;

9、获取滑动隐藏导航栏的手势

@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer;

10、屏幕滑动的时候是否隐藏导航栏,常用于tableView,上滑隐藏导航栏,下滑显示,带动画效果

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe;

11、 弹出键盘的时候是否隐藏导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears;

12、设置横屏的时候隐藏导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact;

13、敲击屏幕可以隐藏与显示导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap;

14、获取敲击屏幕的手势

@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer;

15、导航控制器代理

@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;

16、当一个controller被添加到导航中后,系统会为它分配一些属性
(1)当前controller对应的导航项

@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;

(2)push的时候隐藏底部栏,如push后隐藏tabbar

@property(nonatomic) BOOL hidesBottomBarWhenPushed;

(3)获取管理它的导航控制器

@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController;

上自定义SamTabBar.h代码:

//// EVNSideSlideViewController.h// MMB-Saler//// Created by developer on 16/6/28.// Copyright © 2016年 仁伯安. All rights reserved.//#import "BaseViewController.h"@interface EVNSideSlideViewController : BaseViewController{@private UIImageView * imgBackground; CGFloat scalef;}@property (strong, nonatomic) UIViewController *leftViewController;@property (strong, nonatomic) UIViewController *mainViewController;@property (strong, nonatomic) UIViewController *righViewController;/** * 滑动速度系数,建议在[0.5, 1]之间,默认为0.5 */@property (assign, nonatomic) CGFloat sideSlideSpeed;/** * 初始化,带有左右滑功能的视图 * * @param LeftViewController 左边设置的视图 * @param MainViewController 主视图,可以是TabBarController * @param RighViewController 右滑显示的视图 * @param image 当前视图背景图 * * @return EVNSideSlideViewController object */- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image;/** * 显示主视图控制器 */- showMainViewController;/** * 显示左视图控制器 */- showLeftViewController;/** * 显示右视图控制器 */- showRighViewController;@end

//// EVNSideSlideViewController.m// MMB-Saler//// Created by developer on 16/6/28.// Copyright © 2016年 仁伯安. All rights reserved.//#import "EVNSideSlideViewController.h"@interface EVNSideSlideViewController ()@end@implementation EVNSideSlideViewController- (instancetype)initWithLeftViewController:(UIViewController *)LeftViewController andMainViewController:(UIViewController *)MainViewController andRightViewController:(UIViewController *)RighViewController andBackgroundImage:(UIImage *)image{ self = [super init]; if { _sideSlideSpeed = 0.5; _leftViewController = LeftViewController; _mainViewController = MainViewController; _righViewController = RighViewController; UIImageView * imgview = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds]; [imgview setImage:image]; [self.view addSubview:imgview]; } return self;}- viewDidLoad{ [super viewDidLoad]; /** * 添加滑动手势 */ UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)]; [_mainViewController.view addGestureRecognizer:panGes]; _leftViewController.view.hidden = YES; _righViewController.view.hidden = YES; [self.view addSubview:_leftViewController.view]; [self.view addSubview:_righViewController.view]; [self.view addSubview:_mainViewController.view];}- didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/** * 滑动手势 * * @param panGestureRecognizer 滑动手势 */- handlePan:(UIPanGestureRecognizer *)panGestureRecognizer{ CGPoint point = [panGestureRecognizer translationInView:self.view]; scalef = (point.x*_sideSlideSpeed   scalef); /** * 根据视图位置判断是左滑还是右边滑动 */ if (panGestureRecognizer.view.frame.origin.x >= 0) { panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x   point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y); panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1 - scalef/1000, 1 - scalef/1000); [panGestureRecognizer setTranslation:CGPointMake inView:self.view]; _righViewController.view.hidden = YES; _leftViewController.view.hidden = NO; } else { panGestureRecognizer.view.center = CGPointMake(panGestureRecognizer.view.center.x   point.x*_sideSlideSpeed,panGestureRecognizer.view.center.y); panGestureRecognizer.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1   scalef/1000, 1   scalef/1000); [panGestureRecognizer setTranslation:CGPointMake inView:self.view]; _righViewController.view.hidden = NO; _leftViewController.view.hidden = YES; } /** * 有效手势判断、并调整相应的视图 */ if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) { if (scalef > 120*_sideSlideSpeed) { [self showLeftViewController]; } else if (scalef < - 120*_sideSlideSpeed) { [self showRighViewController]; } else { [self showMainViewController]; scalef = 0; } }}- showMainViewController{ [UIView beginAnimations:nil context:nil]; _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0); _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2.f, [UIScreen mainScreen].bounds.size.height/2.f); [UIView commitAnimations];}- showLeftViewController{ [UIView beginAnimations:nil context:nil]; _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); _mainViewController.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width * 3/2.f - 88.f, [UIScreen mainScreen].bounds.size.height/2.f); [UIView commitAnimations];}- showRighViewController{ [UIView beginAnimations:nil context:nil]; _mainViewController.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); _mainViewController.view.center = CGPointMake(-[UIScreen mainScreen].bounds.size.width/2.f   88.f, [UIScreen mainScreen].bounds.size.height/2.f); [UIView commitAnimations];}@end

#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface CommonHelper : NSObject<NSCopying, NSMutableCopying>@property (strong, nonatomic) UIViewController *rootViewController;   (instancetype)shareInstance;- setObjValue;@end

#import "CommonHelper.h" static id _instance;@implementation CommonHelper///**// * 只要系统中引用了该类,程序运行,就会主动调用load(不用手动调用,而且只会加载1次)// *///  load//{// _instance = [[CommonHelper alloc] init];//}   (instancetype)allocWithZone:(struct _NSZone *)zone{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance;}   (instancetype)shareInstance{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance;}- copyWithZone:zone{ return _instance;}- mutableCopyWithZone:zone{ return _instance;}// 设置当前window的根视图- setObjValue{ [_instance setRootViewController:[[UIApplication sharedApplication] keyWindow].rootViewController];}@end

- tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ CommonHelper *common = [CommonHelper shareInstance]; [common setObjValue]; NSLog(@"%@", NSStringFromClass([common.rootViewController class])); EVNSideSlideViewController *sideSlideViewController = (EVNSideSlideViewController *)common.rootViewController; [sideSlideViewController showMainViewController];// NSLog(@"%@", NSStringFromClass(sideSlideViewController.mainViewController)); BuyerTabbarCtrl *toolController = (BuyerTabbarCtrl *)sideSlideViewController.mainViewController; NSInteger selectIndex = toolController.selectedIndex;// NSLog(@"%@", NSStringFromClass(toolController.viewControllers[0])); // 关于 AboutMaibeiViewController * aboutMaibeiViewController = [[AboutMaibeiViewController alloc] init]; aboutMaibeiViewController.hidesBottomBarWhenPushed = YES; [toolController.viewControllers[selectIndex] pushViewController:aboutMaibeiViewController animated:YES];}

方法

1、创建导航控制器的两种方法
(1)通过一个自定义的导航栏和工具栏创建导航控制器

- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;

(2)使用系统默认的导航栏和工具栏,创建一个导航控制器同时设置一个根视图控制器

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;

2、设置管理的视图控制器

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated;

3、管理视图控制器操作
(1)压入新的视图控制器

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

这个方法是为了iOS方法的命名统一,在导航中,其作用和push一样

- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;

例子:设置隐藏底部TabBar
可以给所有控制器设置一个父类,重写该方法。继承该父类的控制器调用此方法都可以隐藏push来的控制器底部的TabBar

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 这里的参数viewController就是即将被push进来的控制器
    viewController.hidesBottomBarWhenPushed = YES;   
 [super pushViewController:viewController animated:animated];
}

(2)弹出一个视图控制器 返回的是pop的controller

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;

(3)弹出到某个视图控制器 返回所有pop的controller

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

(4)直接pop到根视图控制器,返回所有被pop的controller

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;

4、设置隐藏导航栏

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;

5、是否隐藏底部工具条(默认隐藏)

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;

6、代理方法:
(1)视图将要展示时调用的方法

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

(2)视图已经展示时调用的方法

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

(3)设置导航控制器支持的设备方向

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController;

(4)设置导航控制器的首选设备方向

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController;

(5)下面两个方法可以设置导航的转场动画

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController;
- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC ;
//// SamTabBar.h//#import <UIKit/UIKit.h>// If you need to add other buttons on the tabbar, you need to add corresponding enum types first.typedef NS_ENUM(NSInteger, SamItemType) { SamItemTypeLaunch = 10,// Launch live SamItemTypeLive = 100, // show live SamItemTypeMe, // show me // Other types};@class SamTabBar;@protocol SamTabBarDelegate <NSObject>-tabBar:(SamTabBar *)tabBar clickButton:(SamItemType) index;@end@interface SamTabBar : UITabBar@property(nonatomic, assign) id<SamTabBarDelegate> delegate;@end

在SamTabBar.m中,重写hitTest:方法主要解决中间启动直播按钮引起的不规则问题。

//// SamTabBar.m//#import "SamTabBar.h"@interface SamTabBar()@property(nonatomic, strong) UIButton *cameraButton;@end@implementation SamTabBar// basic implementation...-(instancetype)initWithFrame:frame{ self = [super initWithFrame:frame]; if  { self.translucent = NO; // add items for (NSInteger i = 0; i<self.datalist.count; i  ) { UIButton *item = [UIButton buttonWithType:UIButtonTypeCustom]; item.adjustsImageWhenHighlighted = NO; [item setImage:[UIImage imageNamed:self.datalist[i]] forState:UIControlStateNormal]; [item setImage:[UIImage imageNamed:[self.datalist[i] stringByAppendingString:@"_p"]] forState:UIControlStateSelected]; if  { item.selected = YES; self.lastItem = item; } item.tag = SamItemTypeLive   i; [item addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:item]; } } [self addSubview:self.cameraButton]; return self;}// 调整子视图的布局-layoutSubviews{ [super layoutSubviews]; CGFloat width = self.frame.size.width/self.datalist.count; for (NSInteger i = 0; i<[self subviews].count; i  ) { UIView *btn = [self subviews][i]; if ([btn isKindOfClass:[UIButton class]]) { btn.frame = CGRectMake((btn.tag - SamItemTypeLive)*width, 0, width, self.frame.size.height); } } [self.cameraButton sizeToFit]; self.cameraButton.center = CGPointMake(self.frame.size.width/2, 10);}-hitTest:point withEvent:(UIEvent *)event{// to make sure the tabBar won't respond any touch event when hidden if (self.isHidden) { return nil; } UIView *view = nil; for (UIView *subView in self.subviews) { CGPoint testPoint = [subView convertPoint:point fromView:self]; if (CGRectContainsPoint(subView.bounds, testPoint)) { if (CGRectContainsRect(subView.bounds, self.cameraButton.bounds)) { view = subView; return view; } else if ([subView isKindOfClass:[UIButton class]]) { view = subView; } } } if (view == nil) { view = [super hitTest:point withEvent:event]; } return view;}-clickItem:(UIButton *)button{ if ([self.delegate respondsToSelector:@selector(tabBar:clickButton:)]) { [self.delegate tabBar:self clickButton:button.tag]; } //!self.block?:self.block(self,button.tag); if (self.block) { self.block(self,button.tag); } if (button.tag == SamItemTypeLaunch) { return; } self.lastItem.selected = NO; button.selected = YES; self.lastItem = button; [UIView animateWithDuration:0.2 animations:^{ button.transform = CGAffineTransformMakeScale; } completion:^(BOOL finished) { [UIView animateWithDuration:0.2 animations:^{ button.transform = CGAffineTransformIdentity; }]; }];}

clickItem:为tabbar上button对应的点击事件,里面通过delegate传递消息。-hitTest:point withEvent:(UIEvent *)event方法会在一次事件到来后,从根视图到子视图遍历,找出这个点所在的离手指最近的子视图来响应事件。但是,默认的hitTest:并不能解决落在父视图bounds外的子视图的点击事件,也就是不能解决不规则区域的点击事件,因此,我们需要重载该方法。在本文中,主要需要解决的是中间钮引的不规则问题,所以先用CGPoint testPoint = [subView convertPoint:point fromView:self];将点击坐标从屏幕坐标系,转换到当前遍历到的subView自身坐标系中,再去判断这个subView是否包含了中间的不规则buttonCGRectContainsRect(subView.bounds, self.cameraButton.bounds)。如果包含,则当前点击的就是self.cameraButton,直接返回,终止遍历;否则,点击的可能是左右两个TabBar上的自定义Button,需要再去判断subView是否为UIButton对象;如果遍历完都没有找到对应的subView,则调用默认hitTest:方法。这样一个不规则的TabBar就定义好了。在hitTest:入口加上

 return nil; }```以确保当TabBar被隐藏时,不会响应任何点击事件在TabBarViewController.m中使用自定义的TabBar:

//// SamTabBarViewController.m//

@interface SamTabBarViewController () <SamTabBarDelegate>

@property(nonatomic, strong) SamTabBar* samTabBar;

@end

@implementation SamTabBarViewController

-(SamTabBar *)samTabBar{if (!_samTabBar) {_samTabBar = [[SamTabBar alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height - 50, [UIScreen mainScreen].bounds.size.width, 50)];_samTabBar.delegate = self;}return _samTabBar;}

  • viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.// add controller[self configViewControllers];// add tabbar[[UITabBar appearance] setShadowImage:[UIImage new]];[self setValue:self.samTabBar forKey:@"tabBar"];}

-configViewControllers{//准备子控制器NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"AViewController",@"BViewController"]];for (NSInteger i = 0; i<array.count; i ) {NSString *vcName = array[i];UIViewController *vc = [[NSClassFromString alloc]init];UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:vc];[array replaceObjectAtIndex:i withObject:nav];}self.viewControllers = array;}

-tabBar:(SamTabBar *)tabBar clickButton:(SamItemType)index{if (index != SamItemTypeLaunch) {self.selectedIndex = index - SamItemTypeLive;} else if(index == SamItemTypeLaunch) {[self.launchOptionView showUp];}}

在viewDidLoad方法中,实现```-tabBar:(SamTabBar *)tabBar clickButton:(SamItemType)index```方法,用于正确显示需要显示的viewController。使用``` [self setValue:self.samTabBar forKey:@"tabBar"];```方法,就可以用自定义TabBar替换默认TabBar。稍微完整的Demo在[这里。](https://github.com/SamChenzx/inke_trick)
TAG标签:
版权声明:本文由美高梅网投平台发布于美高梅网投网址,转载请注明出处:导航控制器,自定义一个形状不规则的TabBar