2.自定义标签栏
1.创建多个视图控制器,抽出一个类叫MainTabCtrl 继承于
UITabBarController
在AppDelegate.m中创建
MainTabCtrl对象
self.windows.roo… =
MainTabCtrl对象
//————————————————————--------------—创建控制器---------------------------------
//1.创建控制器
- (void)_initCtrls {
//1.创建视图控制器
FirstViewController *firstCtrl = [[FirstViewController alloc] init];
SecondViewController *secondCtrl = [[SecondViewController alloc] init];
ThirdViewController *thirdCtrl = [[ThirdViewController alloc] init];
ForthViewController *forthCtrl = [[ForthViewController alloc] init];
FifthViewController *fifthCtrl = [[FifthViewController alloc] init];
NSArray *viewCtrls = @[firstCtrl,secondCtrl,thirdCtrl,forthCtrl,fifthCtrl];
//2.创建导航控制器
NSMutableArray *navCtrls = [[NSMutableArray alloc] init];
for (int i=0; i<viewCtrls.count; i++) {
//取得视图控制器
UIViewController *viewCtrl = viewCtrls[i];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
[navCtrls addObject:navCtrl];
}
//3.交给标签控制器管理
self.viewControllers = navCtrls;
}
//————————————————————————————自定义tabbar---------------------------------
//2.自定义标签栏
- (void)_initTabbar {
//1.移除标签栏上的默认按钮
NSArray *array = self.tabBar.subviews;
for (UIView *subView in array) {
//判断subView是否属于UITabBarButton这个类
//将字符串转换成类
if ([subView isKindOfClass: NSClassFromString(@"UITabBarButton")]) {
[subView removeFromSuperview];
}
}
//2.设置标签栏的背景图片
[self.tabBar setBackgroundImage:[UIImage imageNamed:@"navbg.png"]];
//3.添加按钮、添加点击事件
CGFloat width = [UIScreen mainScreen].bounds.size.width/5.0;
for (int i=0; i<5; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//设置显示的图片
NSString *imgName = [NSString stringWithFormat:@"%d.png",i+1];
[button setImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
//设置tag
//如果想通过tag值取得对应的视图,tag不可以设置在100以下
button.tag = i;
//添加点击事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//设置frame
button.frame = CGRectMake((width-42)/2.0+width*i, (49-40)/2.0, 42, 40);
[self.tabBar addSubview:button];
}
//4.添加选中图标
_selectImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"选中.png"]];
_selectImg.frame = CGRectMake((width-53)/2.0, (49-45)/2.0, 53, 45);
[self.tabBar addSubview:_selectImg];
}
//---------------------------------按钮触发事件-----------------------------
- (void)buttonAction:(UIButton *)button {
//切换视图控制器
NSInteger tag = button.tag;
self.selectedIndex = tag;
[UIView animateWithDuration:.35 animations:^{
//切换_selectImg的位置
_selectImg.center = button.center;
}];
}
//————————————————跳转时隐藏标签栏导航栏push按钮 进入下一页面并隐藏tabBar
-----------------------------
在要push到的那个视图控制器重写初始化方法
- (
instancetype
)init
{
self = [ super init ];
if ( self ) {
self . hidesBottomBarWhenPushed = YES ;
}
return self ;
{
self = [ super init ];
if ( self ) {
self . hidesBottomBarWhenPushed = YES ;
}
return self ;
}
3.标签控制器默认创建方式
//1.新建4个视图控制器对象
HomeViewController *homePage = [[HomeViewController alloc] init];
homePage.title = @"首页";
MessageViewController *message = [[MessageViewController alloc] init];
message.title = @"信息";
SetupViewController *setup = [[SetupViewController alloc] init];
setup.title = @"设置";
MoreViewController *more = [[MoreViewController alloc] init];
more.title = @"更多";
//2.将视图控制器加入数组
NSArray *viewControllers = @[homePage, message, setup, more];
//3.将数组交给标签控制器对象
UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = viewControllers;
//4.添加到windows
self.window.rootViewController = tabBar;
4.常用属性和方法
4.1
//分栏控制器的属性
//1.
修改当前显示的子控制器
//self代表标签控制器
self.selectedIndex = button.tag;
//tabBar属性
//1.
选中按钮颜色
tabBar.
tabBar
.
tintColor
= [
UIColor
redColor
];
//2.
设置背景色
tabBar.
tabBar
.
backgroundColor
= [
UIColor
redColor
];
//tabBaritem属性
message.
tabBarItem
.
badgeValue
=
@"10"
;
//设置选中图片,返回一张原始的图片
childVc.tabBarItem.selectedImage =
[[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//
给标签栏按钮设置图片
homePage.tabBarItem.image = [UIImage imageNamed:];