ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITabBarDelegate>
@end
ViewController.m文件
#import "ViewController.h"
@interface ViewController ()
{
UIView * _view0; //TabBar上面的视图
}
@end
@implementation ViewController
//产生动画效果变化包括:1.位置变化 2.大小变化 3.拉伸变化 4.改变透明度 5.改变状态 6.改变视图顺序 7.旋转
- (void)viewDidLoad {
[super viewDidLoad];
//设置屏幕整体背景颜色
self.view.backgroundColor=[UIColor whiteColor];
//加载TabBar
[self _loadTabBar];
//加载view
[self _loadView];
}
- (void) _loadView
{
//4个视图分别对应 TabBar的四个item 视图的高度 + TabBar的高度 = 屏幕的高度
UIView * view1=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
view1.backgroundColor=[UIColor redColor];
UIView * view2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
view2.backgroundColor=[UIColor yellowColor];
UIView * view3=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
view3.backgroundColor=[UIColor blueColor];
UIView * view4=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
view4.backgroundColor=[UIColor greenColor];
_view0=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
//_view0.backgroundColor=[UIColor whiteColor];
_view0.tag = 200;
[self.view addSubview:_view0];
[_view0 addSubview:view2];
[_view0 addSubview:view3];
[_view0 addSubview:view4];
[_view0 addSubview:view1];
view1.tag = 201;
view2.tag = 202;
view3.tag = 203;
view4.tag = 204;
}
#pragma mark - 加载TabBar
- (void) _loadTabBar
{
//TabBar 放在屏幕最下面--类似于导航栏 item 的个数没有限制
//设置TabBar的位置及长宽
UITabBar * tabBar=[[UITabBar alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-49, self.view.frame.size.width, 49)];
tabBar.delegate = self;
//TabBar设置
/*
//透明效果----改变背景色
tabBar.backgroundColor=[UIColor redColor];
//改变样式
tabBar.barStyle=UIBarStyleBlack;
//设置背景图片
tabBar.backgroundImage=[UIImage imageNamed:@"bg1.jpg"];
*/
//添加TabBar上的 item 类似于button 实质都是UIView
//1>添加方法1 定义UITabBarItem ,并加到数组
UITabBarItem * tb1=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:101];
UITabBarItem * tb2=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:102];
//2>添加方法2 添加UITabBarItem 的 第三个item 自定义图片,未选中时保持原来的颜色
UIImage * img1=[[UIImage imageNamed:@"star_top250"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage * img2=[UIImage imageNamed:@"star_top250"];
UITabBarItem * tb3=[[UITabBarItem alloc]initWithTitle:@"hello" image:img1 selectedImage:img2];
tb3.tag = 103;
//3>添加方法3
UITabBarItem * tb4=[[UITabBarItem alloc]initWithTitle:@"worldworldddddddddd" image:[UIImage imageNamed:@"msg_new"] tag:104];
//修改小图标下面的名字
NSDictionary * dic=@{NSFontAttributeName:[UIFont systemFontOfSize:17]};
[tb1 setTitleTextAttributes:dic forState:UIControlStateNormal];
[tb2 setTitleTextAttributes:dic forState:UIControlStateNormal];
[tb3 setTitleTextAttributes:dic forState:UIControlStateNormal];
[tb4 setTitleTextAttributes:dic forState:UIControlStateNormal];
NSArray * array=@[tb1,tb2,tb3,tb4];
tabBar.items=array;
tabBar.selectedItem=array[0];
//添加到主视图
[self.view addSubview:tabBar];
}
#pragma mark - 代理方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSInteger tag = item.tag;
UIView * view = [self.view viewWithTag:tag+100];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:_view0 cache:YES];
[_view0 bringSubviewToFront:view];
[UIView commitAnimations];
}
@end