UITabBar

1 篇文章 0 订阅

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>添加方法定义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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值