大体思路:1.创建一个UITabBarController类
2.创建一个UINavigationController类
3.创建一个BaseVIewController类(UiviewController),即几个切换的视图都继承于它,方便设置公共属性,减少代码量。
4.创建几个子视图
5.重要代码如下,其他的可以自己设置
6.效果图
��
//
// TabBarViewController.m
// _AliXQPlayer
//
// Created by mac on 16/7/16.
// Copyright © 2016年 mac. All rights reserved.
//
//通过底部的标签按钮实现几个视图相互切换
#import "TabBarViewController.h"
//导航控制器
#import "BaseNavigViewController.h"
//四个子视图
#import "DynamicViewController.h"
#import "DiscoverViewController.h"
#import "MessageViewController.h"
#import "MineViewController.h"
//定义屏幕的高宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
//定义tabBar的高度
#define kTabBarHeight 49
@interface TabBarViewController ()
@end
@implementation TabBarViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.创建子视图
[self createSubviews];
//2.移除系统自带的子视图
[self removeTabBarSubviews];
//3.创建自定义按钮
[self createBarButton];
}
#pragma mark - 创建子视图
- (void) createSubviews {
//1.创建动态页面
DynamicViewController *dynamicVC = [[DynamicViewController alloc] init];
//2.创建发现页面
DiscoverViewController *discoverVC = [[DiscoverViewController alloc] init];
//3.创建消息页面
MessageViewController *messageVC = [[MessageViewController alloc] init];
//4.创建我的页面
MineViewController *mineVC = [[MineViewController alloc] init];
//5.创建数组,存储这些视图
NSArray *viewVCs = @[dynamicVC,discoverVC,messageVC,mineVC];
//6.创建可变数组,存储导航控制器
NSMutableArray *navigVCs = [NSMutableArray array];
//7.利用循环,将视图添加导航控制器
for (int i = 0; i < viewVCs.count; i++) {
//1.创建导航控制器
BaseNavigViewController *navi = [[BaseNavigViewController alloc] initWithRootViewController:viewVCs[i]];
//2.将创建的添加到数组
[navigVCs addObject:navi];
}
//8.将数组赋值给viewControllers
self.viewControllers = navigVCs;
}
#pragma mark - 创建自定义按钮
- (void) createBarButton {
//1.设置tabBar的背景图片
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kTabBarHeight)];
bgImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"tabBarViewBg"]];
[self.tabBar addSubview:bgImageView];
//1.创建数组存储图片名
NSArray *imageNameArray = @[@"trends",@"find",@"message",@"my"];
//2.利用循环创建按钮
for (int i = 0; i < 4; i++) {
//a) 创建按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//b) 设置按钮frame
button.frame = CGRectMake(i*(kScreenWidth/4), 0, kScreenWidth/4,kTabBarHeight);
//c) 设置按钮普通状态下的背景图片
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"trends_%@_normal",imageNameArray[i]]] forState:UIControlStateNormal];
//d) 设置按钮选中状态下的背景图片
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"trends_%@_click",imageNameArray[i]]] forState:UIControlStateSelected];
//e) 设置tag值
button.tag = 1000 + i;
//f) 设置点击事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//g) 添加到视图
[self.tabBar addSubview:button];
//h) 设置默认选中项
if (i == 0) {
button.selected = YES;
}
}
}
#pragma mark - 按钮点击事件
- (void) buttonAction:(UIButton *) button{
//1.获取按钮的tag值
NSInteger index = button.tag-1000;
//2.根据tag值,切换视图
self.selectedIndex = index;
//3.切换按钮的选中状态,判断当前页面是否和tag值一致,如果一致,则选中
for (int i = 0; i < self.viewControllers.count; i++) {
//a)复制tabBar上的按钮
UIButton *btn = (UIButton *)[self.tabBar viewWithTag:1000+i];
//b) 判断
if (btn.tag == button.tag) {
btn.selected = YES;
}else {
btn.selected = NO;
}
}
}
#pragma mark - 移除系统自带的子视图
- (void) removeTabBarSubviews {
//1.通过forIn取出每一个子视图,然后进行判断移除
//a) 判断一:[类 class]
//b) 判断二:NSClassFromString(@"")
for (UIView *view in self.tabBar.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
[view removeFromSuperview];
}
}
}
@end
//
// BaseViewController.m
// _AliXQPlayer
//
// Created by mac on 16/7/16.
// Copyright © 2016年 mac. All rights reserved.
//
//实现自定义按钮添加到导航栏上
#import "BaseViewController.h"
//定义屏幕的高宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface BaseViewController ()
@end
@implementation BaseViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.设置背景颜色
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"view-background"]];
//2.创建左边导航栏的按钮
//a) 创建按钮
UIButton *LeftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//b) 设置frame
LeftButton.frame = CGRectMake(0, 0, 20, 20);
//c) 设置背景图片
[LeftButton setBackgroundImage:[UIImage imageNamed:@"lyric_search"] forState:UIControlStateNormal];
//d) 设置点击事件
[LeftButton addTarget:self action:@selector(LeftButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//e) 设置UIBarButtonItem
UIBarButtonItem *leftButtonItem = [[UIBarButtonItem alloc] initWithCustomView:LeftButton];
//f) 添加到导航栏
self.navigationItem.leftBarButtonItem = leftButtonItem;
//3.创建右边导航栏的按钮
//a) 创建按钮
UIButton *rightButton = [UIButton buttonWithType: UIButtonTypeCustom];
//b)设置frame
rightButton.frame = CGRectMake(kScreenWidth-20, 0, 20, 20);
//c) 设置背景图片
[rightButton setBackgroundImage:[UIImage imageNamed:@"SquareSeekRingNor"] forState:UIControlStateNormal];
//d) 设置点击事件
[rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//e) 设置UIBarButtonItem
UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
//f) 添加到导航栏
self.navigationItem.rightBarButtonItem = rightButtonItem;
}
#pragma mark - leftButton点击事件
- (void) LeftButtonAction:(UIButton *) button {
}
#pragma mark - rightButton点击事件
- (void) rightButtonAction:(UIButton *) button {
}
@end