UITabBar

来源:互联网 发布:js value 编辑:程序博客网 时间:2024/05/01 02:21

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 {

    [superviewDidLoad];

    

    //设置屏幕整体背景颜色

    self.view.backgroundColor=[UIColorwhiteColor];

    

    //加载TabBar

    [self_loadTabBar];


    //加载view

    [self_loadView];

}



- (void) _loadView

{

    //4个视图分别对应 TabBar的四个item  视图的高度 + TabBar的高度 =屏幕的高度

   UIView * view1=[[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-49)];

    view1.backgroundColor=[UIColorredColor];

    

   UIView * view2=[[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-49)];

    view2.backgroundColor=[UIColoryellowColor];

    

   UIView * view3=[[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-49)];

    view3.backgroundColor=[UIColorblueColor];

    

   UIView * view4=[[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-49)];

    view4.backgroundColor=[UIColorgreenColor];

    

    

    

    _view0=[[UIViewalloc]initWithFrame:CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height-49)];

    //_view0.backgroundColor=[UIColor whiteColor];

   _view0.tag =200;

     [self.viewaddSubview:_view0];

    

    

    [_view0addSubview:view2];

    [_view0addSubview:view3];

    [_view0addSubview:view4];

    [_view0addSubview:view1];

   

    view1.tag =201;

    view2.tag =202;

    view3.tag =203;

    view4.tag =204;

}



#pragma mark - 加载TabBar

- (void) _loadTabBar

{

    //TabBar   放在屏幕最下面--类似于导航栏   item 的个数没有限制

    

    //设置TabBar的位置及长宽

   UITabBar * tabBar=[[UITabBaralloc]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=[[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemFavoritestag:101];

    UITabBarItem * tb2=[[UITabBarItemalloc]initWithTabBarSystemItem:UITabBarSystemItemBookmarkstag:102];

    

    //2>添加方法2  添加UITabBarItem  第三个item  自定义图片,未选中时保持原来的颜色

    UIImage * img1=[[UIImageimageNamed:@"star_top250"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

   UIImage * img2=[UIImageimageNamed:@"star_top250"];

    UITabBarItem * tb3=[[UITabBarItemalloc]initWithTitle:@"hello"image:img1 selectedImage:img2];

    tb3.tag =103;

    

    //3>添加方法3

    UITabBarItem * tb4=[[UITabBarItemalloc]initWithTitle:@"worldworldddddddddd"image:[UIImageimageNamed:@"msg_new"]tag:104];

    

    //修改小图标下面的名字

    NSDictionary * dic=@{NSFontAttributeName:[UIFontsystemFontOfSize:17]};

    [tb1 setTitleTextAttributes:dicforState:UIControlStateNormal];

    [tb2 setTitleTextAttributes:dicforState:UIControlStateNormal];

    [tb3 setTitleTextAttributes:dicforState:UIControlStateNormal];

    [tb4 setTitleTextAttributes:dicforState:UIControlStateNormal];

    

    

   NSArray * array=@[tb1,tb2,tb3,tb4];

    tabBar.items=array;

    

    tabBar.selectedItem=array[0];

    //添加到主视图

    [self.viewaddSubview:tabBar];

}


#pragma mark - 代理方法

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

   NSInteger tag = item.tag;

   UIView * view = [self.viewviewWithTag:tag+100];

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:1];

    [UIViewsetAnimationTransition:UIViewAnimationTransitionFlipFromLeftforView:_view0cache:YES];

    [_view0bringSubviewToFront:view];

    [UIViewcommitAnimations];

}


@end



0 0
原创粉丝点击