- //
- //
XNTabBarController.m - //
- //
- //
Created by neng on 14-6-19. - //
Copyright (c) 2014年 neng. All rights reserved. - //
-
- #import
"XNTabBarController.h" - #import
"Common.h" - #import
"XNTabBarButton.h" -
- @interface
XNTabBarController () -
- @property
(nonatomic, weak) UIButton *selectedBtn; - @end
-
- @implementation
XNTabBarController -
- -
(void)viewDidLoad { -
[super viewDidLoad]; -
//下面两个方法在开发中是经常会用到的 - //
NSLog(@"%s",__func__); - //
NSLog(@"%@",self.view.subviews); //能打印出所有子视图,和其frame -
LogFun; -
LogSubviews(self.view); -
-
-
//删除现有的tabBar -
CGRect rect = self.tabBar.frame; -
[self.tabBar removeFromSuperview]; //移除TabBarController自带的下部的条 -
-
//测试添加自己的视图 -
UIView *myView = [[UIView alloc] init]; -
myView.frame = rect; -
myView.backgroundColor = [UIColor redColor]; -
[self.view addSubview:myView]; -
-
for (int i = 0; i < 5; i++) { -
//UIButton *btn = [[UIButton alloc] init]; -
XNTabBarButton *btn = [[XNTabBarButton alloc] init]; -
-
NSString *imageName = [NSString stringWithFormat:@"TabBar%d", i + 1]; -
NSString *imageNameSel = [NSString stringWithFormat:@"TabBar%dSel", i + 1]; -
-
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal]; -
[btn setImage:[UIImage imageNamed:imageNameSel] forState:UIControlStateSelected]; -
-
CGFloat x = i * myView.frame.size.width / 5; -
btn.frame = CGRectMake(x, 0, myView.frame.size.width / 5, myView.frame.size.height); -
-
[myView addSubview:btn]; -
-
btn.tag = i;//设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图 -
-
//带参数的监听方法记得加"冒号" -
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpIns ide]; -
-
//设置刚进入时,第一个按钮为选中状态 -
if (0 == i) { -
btn.selected = YES; -
self.selectedBtn = btn; //设置该按钮为选中的按钮 -
} -
} - }
-
-
- -
(void)clickBtn:(UIButton *)button { -
//1.先将之前选中的按钮设置为未选中 -
self.selectedBtn.selected = NO; -
//2.再将当前按钮设置为选中 -
button.selected = YES; -
//3.最后把当前按钮赋值为之前选中的按钮 -
self.selectedBtn = button; -
-
//4.跳转到相应的视图控制器. (通过selectIndex参数来设置选中了那个控制器) -
self.selectedIndex = button.tag; - }
-
- @end
XNTabBarButton.h
- #import
-
- @interface
XNTabBarButton : UIButton -
- @end
XNTabBarButton.m
- #import
"XNTabBarButton.h" -
- @implementation
XNTabBarButton -
- -
(void)setHighlighted:(BOOL)highlighted{ - //
[super setHighlighted:highlighted]; - }
-
- @end