有時候項目需求TabBar在側邊,不在底部,當需要的時候顯示出來,不需要的時候隱藏
基于這種需求,自定義一個側邊欄和UITabController
效果.gif
第一步:
首先封裝一個側邊欄的控件
.h文件
#import lt;UIKit/UIKit.hgt; @protocol LeftViewDelegate lt;NSObjectgt; /** 點擊側邊欄按鈕調用方法(必寫) @param selectedIndex 按鈕tag */ @required - (void)didClickChildButton:(int)selectedIndex; @end @interface LeftView : UIView @property(nonatomic, strong) NSArray *itemArray; @property(nonatomic, strong) id lt;LeftViewDelegategt;delegate; @end @interface TabButton : UIButton @end
.m文件
#import quot;LeftView.hquot; #define VIEW_WIDTH self.bounds.size.width #define VIEW_HEIGHT self.bounds.size.height @interface LeftView() @property(nonatomic, strong) UIButton *selectedButton; @end @implementation LeftView -(instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self setUp]; } return self; } - (void)setUp { self.backgroundColor = [UIColor whiteColor]; } - (void)setItemArray:(NSArray *)itemArray { _itemArray = itemArray; int n = 0; for (NSDictionary *dict in itemArray) { TabButton *button = [TabButton buttonWithType:UIButtonTypeCustom]; [button setTitle:dict[@quot;titlequot;] forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:dict[@quot;imagequot;]] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:dict[@quot;selectImagequot;]] forState:UIControlStateSelected]; button.tag = 999 n; [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:button]; // 默認第一個按鈕點擊,跟tabbarController默認選中的控制器index一致 if (n == 0) {// 0 self.selectedButton = button; self.selectedButton.selected = YES; } n ; } } - (void)buttonClick:(UIButton *)button { int tag = (int)button.tag - 999; NSLog(@quot;點擊了%d個按鈕quot;,tag); if ([self.delegate respondsToSelector:@selector(didClickChildButton:)]) { [self.delegate didClickChildButton:tag]; self.selectedButton.selected = NO; self.selectedButton = button; self.selectedButton.selected = YES; } } -(void)layoutSubviews { CGFloat height = [UIScreen mainScreen].bounds.size.height / 4; for (UIView *child in self.subviews) { Class class = NSClassFromString(@quot;UIButtonquot;); if ([child isKindOfClass:class]) { int tag = (int)child.tag - 999; child.frame = CGRectMake(0, height * tag, VIEW_WIDTH, height); } } } @end #pragma mark - 自定義tabBar按鈕 @implementation TabButton - (void)layoutSubviews{ [super layoutSubviews]; self.imageView.frame = CGRectMake(0, VIEW_HEIGHT * 0.1, VIEW_WIDTH, VIEW_HEIGHT * 0.5); self.imageView.contentMode = UIViewContentModeScaleAspectFit; self.titleLabel.frame = CGRectMake(0, VIEW_HEIGHT * 0.6, VIEW_WIDTH, VIEW_HEIGHT * 0.2); self.titleLabel.textAlignment = NSTextAlignmentCenter; } @end
第二步:自定義UITabController
.m文件
#import quot;MainTabBarController.hquot; #import quot;OneViewController.hquot; #import quot;TwoViewController.hquot; #import quot;ThreeViewController.hquot; #import quot;FourViewController.hquot; #import quot;LeftView.hquot; // 側邊欄的寬度 #define LEFT_WIDTH 100 @interface MainTabBarController ()lt;LeftViewDelegategt; @property(nonatomic, strong) LeftView *lefeView; @property(nonatomic, strong) UIView *bgView; @property (assign, nonatomic,getter=isHidden) BOOL hidden; // tabBar 的標題 圖片字典數組 @property(nonatomic, strong) NSMutableArray *tabItems; @end @implementation MainTabBarController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // init child vc NSMutableArray *array = [NSMutableArray array]; OneViewController *onevc = [[OneViewController alloc] init]; TwoViewController *twovc = [[TwoViewController alloc] init]; ThreeViewController *threevc = [[ThreeViewController alloc] init]; FourViewController *fourvc = [[FourViewController alloc] init]; [self setUpChildViewController:onevc array:array title:@quot;話題quot; imageName:@quot;tabbar_topicquot; selectImageName:@quot;tabbar_topic_selectedquot;]; [self setUpChildViewController:twovc array:array title:@quot;材料quot; imageName:@quot;tabbar_materialquot; selectImageName:@quot;tabbar_material_selectedquot;]; [self setUpChildViewController:threevc array:array title:@quot;表單quot; imageName:@quot;tabbar_formquot; selectImageName:@quot;tabbar_form_selectedquot;]; [self setUpChildViewController:fourvc array:array title:@quot;更多quot; imageName:@quot;tabbar_morequot; selectImageName:@quot;tabbar_more_selectedquot;]; self.viewControllers = array; self.selectedIndex = 0; self.hidden = YES; // ori tabbar set nil ...... [self setValue:nil forKeyPath:@quot;tabBarquot;]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)setUpChildViewController:(UIViewController *)viewController array:(NSMutableArray *)array title:(NSString *)title imageName:(NSString *)imageName selectImageName:(NSString *)selectImageName { UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:viewController]; viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1.0]; viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@quot;item.pngquot;] style:UIBarButtonItemStylePlain target:self action:@selector(tabHiddenOrShow)]; [array addObject:navVC]; NSDictionary *dict = @{@quot;titlequot;:title, @quot;imagequot;:imageName, @quot;selectImagequot;:selectImageName }; [self.tabItems addObject:dict]; } - (void)tabHiddenOrShow { [self.tabBarController.tabBar setHidden:YES]; self.hidden = !self.isHidden; if (self.lefeView == nil) { self.lefeView = [[LeftView alloc] initWithFrame:CGRectMake(-LEFT_WIDTH, 0, LEFT_WIDTH, [UIScreen mainScreen].bounds.size.height)]; self.lefeView.delegate = self; self.lefeView.itemArray = self.tabItems; [[UIApplication sharedApplication].keyWindow addSubview:self.lefeView]; } if (self.bgView == nil) { self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)]; self.bgView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:0.5]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)]; [self.bgView addGestureRecognizer:tap]; } CGRect leftFrame = self.lefeView.frame; if (self.isHidden == YES) { leftFrame.origin.x = -LEFT_WIDTH; [self.bgView removeFromSuperview]; } else { [[UIApplication sharedApplication].keyWindow insertSubview:self.bgView belowSubview:self.lefeView]; leftFrame.origin.x = 0; } [UIView animateWithDuration:0.5 animations:^{ self.lefeView.frame = leftFrame; [self.view setNeedsLayout]; }]; } - (void)tapClick:(UITapGestureRecognizer *)tap { [self tabHiddenOrShow]; } #pragma mark - LeftViewDelegate -(void)didClickChildButton:(int)selectedIndex { self.selectedIndex = selectedIndex; [self tabHiddenOrShow]; } #pragma mark - set amp; get -(NSMutableArray *)tabItems { if (_tabItems == nil) { _tabItems = [NSMutableArray array]; } return _tabItems; } @end
在你需要添加 UITabController
的地方使用
我這里在APPDELAGE里使用到的
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; MainTabBarController *mainVC = [[MainTabBarController alloc]init]; self.window.rootViewController = mainVC; [self.window makeKeyAndVisible];
Tags: iOS開發
文章來源:http://www.jianshu.com/p/a2e80c552c47