做一个项目前搭建一个tabBar(一)框架

前言

通常做一个项目前,不算开始讨论需求,分析产品等等,一开始会给我们搭建一个框架,今天简单说一下搭建框架.

github网址:https://github.com/Moonths/iWatch.git

效果图

正文

1.创建几个ViewController添加到TabBarController上,一般添加3-4个tabBar,最多不过5个,5个以上之后用户体验就不好了.

2.创建好之后依次引入头文件,在viewDidLoad上添加

  
      //TalkViewController.h
    TalkViewController *talkVC = [[TalkViewController alloc] init];
    UINavigationController *talkNC = [[UINavigationController alloc] initWithRootViewController:talkVC];
    talkNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"iconfont-12.png"] selectedImage:[UIImage imageNamed:@"iconfont-12-1.png"] ];
    
    
    //MessageViewController.h
    MessageViewController *messageVC = [[MessageViewController alloc] init];
    UINavigationController *messageNC = [[UINavigationController alloc] initWithRootViewController:messageVC];
    messageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"活动" image:[UIImage imageNamed:@"iconfont-4.png"]   selectedImage:[UIImage imageNamed:@"iconfont-4-1.png"] ];
    
    
    //MeViewController.h
    MeViewController *meVC = [[MeViewController alloc] init];
    UINavigationController *meNC = [[UINavigationController alloc] initWithRootViewController:meVC];
    meNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"iconfont-wo.png"]  selectedImage:[UIImage imageNamed:@"iconfont-wo-1.png"]]];
    
    self.viewControllers = @[talkNC,messageNC,meNC];
    

 3.改变属性

上面的代码点击tabBar的时候并不是你放上的图片颜色,要加一个属性

  talkNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"消息" image:[UIImage imageNamed:@"iconfont-12.png"] selectedImage:[[UIImage imageNamed:@"iconfont-12-1.png"]  imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal]];
 messageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"活动" image:[[UIImage imageNamed:@"iconfont-4.png"]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-4-1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
 meNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[[UIImage imageNamed:@"iconfont-wo.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:@"iconfont-wo-1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

 4.改变下面tabBar的背景颜色

  //改变tabBar的背景颜色
   UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
    backView.backgroundColor = [UIColor grayColor];
    [self.tabBar insertSubview:backView atIndex:0];
    //opaque默认是YES 不透明的意思
    self.tabBar.opaque = YES;

 5.改变tabBar上的字体颜色

    //设置字体的颜色
    self.tabBar.tintColor = [UIColor whiteColor];

miaomiaoccat小语

这个代码比较简单,我就没有传到github上,上面基本是全部代码,应该可以完成,有不对的地方欢迎指教.请留言.

原文地址:https://www.cnblogs.com/miaomiaocat/p/5034352.html