基本设置

  • 设置APPIcon(直接拖图片)
  • 设置启动图片
    • launch Screen File里的LaunchScreen.xib给删掉

    • 点击launch image source框内的Use Asset Catalog

,点击Migrate就会将启动图片整合到蓝色资源文件夹里,就像APPIcon一样将图片都拖到LauncImage里

  • 更改软件名,将01-百思不得姐改为百思不得姐(两种方法)

    • info.plist里修改,将key为Bundle name的value改为百思不得姐

    • 或点击蓝色的工程区域,同样在info中修改Bundle name

  • 这个项目采用代码加xib的形式完成,将storyboard删掉后要点击蓝色项目将Main interface里的Main删掉

  • 来到Appdelegate.m文件,在-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions这个方法中进行初始化(如:创建窗口), 这个方法在程序启动完成后会调用一次。
  • 初始化窗口

    // 创建窗口
    self.window = [[UIWindow alloc] init];
    self.window.frame = [UIScreen mainScreen].bounds;
    //设置窗口的根控制器(注意要导入ViewController.h头文件)
    self.window.rootViewController = [[ViewCotroller alloc] init];
    // 显示窗口
    [self.window makeKeyAndVisible];
  • 在AppDelegate.m文件中设置跟控制器view的背景色

    self.view.backgroundColor = [UIColor redColor];

    配置tabBar

  • 在使用git管理代码,每次实现一个功能后点击Source Control,选择Commit,写入commit message,并commit files。
  • 搭建骨架,添加tabBar
    • 根控制器修改为UITabBarController

      // 设置窗口的根控制器
      self.window.rootViewController = [[UITabBarController alloc] init];
    • 添加子控制器

      // 设置窗口的根控制器
      UITabBarController *tabBarController = [[UITabBarController alloc] init];
      // 添加子控制器(添加4个字控制器,代码只写一个示例)
      UIViewController *vc01 = [[UIViewController alloc] init];
      vc01.view.backgroundColor = [UIColor grayColor];    [tabBarController addChildViewController:vc01];
      self.window.rootViewController = tabBarController;
    • tabBarItem属性设置tabBar上的icon和title
      • 将项目图片都拖到蓝色Images.xcassets文件夹中
      • 设置icon和title
        objc vc01.tabBarItem.title = @"精华"; vc01.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"]; vc01.tabBarItem.selectedImage = [UIImage imageNamed:@"tabBar_essence_click_icon"];
  • 添加子控制器的代码不应该放在APPDelegate.m文件里,要放在自定义tabBarController.(封装
    • create一个自定义控制器ADMTabBarController,继承自UITabBarController,将根控制器设置为自己的tarBarController

      self.window.rootViewController = [[ADMTabBarController alloc] init];
  • 点击选中的icon和title会被默认自动渲染为蓝色(两种解决方法)
    • imageWithRenderingMode方法,选择UIImageRenderingModeAlwaysOriginal属性.

      // 设置icon
      UIImage *image = [UIImage imageNamed:@"tabBar_essence_click_icon"];
      image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
      vc01.tabBarItem.selectedImage = image;
    • 或直接在项目的图片的image set上面设置Render为Original Image,一劳永逸(建议用这种)

  • 设置title(两种方法)
    • NSForegroundColorAttributeNameNSFontAttributeName

      // 属性存放在可变字典中
      NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
      attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
      attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
      [vc01.tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
      
      NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
      selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
      selectedAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
      [vc01.tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    • appearance来设置,一劳永逸(建议用这种)

        // 通过appearance统一设置所有UITabBarItem的文字属性
        // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置
        UITabBarItem *item = [UITabBarItem appearance];
        [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
        [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
  • appearance

    // 通过appearance统一设置所有UITabBarItem的文字属性
    // 后面带有UI_APPEARANCE_SELECTOR的方法, 都可以通过appearance对象来统一设置。如:
    - (void)setTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state      NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
    - (NSDictionary *)titleTextAttributesForState:(UIControlState)state      NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;
    //使用
    [UITabBarItem appearance];
    [UINavigationBar appearance];

自定义子控制器

  • 写一个方法将重复的代码封装起来
  • 方法如下
      //添加子控制器
      [self seuupChildVc:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
         /**
           *初始化子控制器
           */
      -(void)setupChildVc:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
      {
        // 设置文字和图片
          vc.tabBarItem.title = title;
          vc.tabBarItem.image = [UIImage imageNamed:image];
          vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
          vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0                         blue:arc4random_uniform(100)/100.0 alpha:1.0];
          // 添加为子控制器
          [self addChildViewController:vc];
        }
  • 为了方法的扩展性,方法改为(建议):
  //添加子控制器
    [self setupChildVc:[[XMGEssenceViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];

    [self setupChildVc:[[XMGNewViewController alloc] init] title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];

    [self setupChildVc:[[XMGFriendTrendsViewController alloc] init] title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];

    [self setupChildVc:[[XMGMeViewController alloc] init] title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];

 /**
   *初始化子控制器
   */
  - (void)setupChildVc:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
  {
    // 设置文字和图片
      vc.tabBarItem.title = title;
      vc.tabBarItem.image = [UIImage imageNamed:image];
      vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImage];
      vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];
      // 添加为子控制器
      [self addChildViewController:vc];
    }

自定义tabBar

  • 这个是UITabBarController里面的TabBar,注意该属性是readonly的。
  @property(nonatomic,readonly) UITabBar *tabBar NS_AVAILABLE_IOS(3_0); // Provided for -[UIActionSheet showFromTabBar:]. Attempting to modify the contents of the tab bar directly will throw an exception.
  • command+R运行后,点击调试工具,在左边点击UIWindow,选择UITabBar,展开后可看tabBar的内部结构。

  • 尝试直接在tabBar上添加按钮

  //添加加号发布按钮
  UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCoustom];
  [publishButton setBackgroundImage:[UIImage imageNamed:@"tabB_publish_icon"] forState:UIControlStateNormal];
  [publishButton setBackgroundImage:[UIImage imageNamed:@"tab_publish_click_icon"] forState:UIControlStateHighlighted];
  publishButton.bounds = CGRectMake(0, 0, publishButton.currentBackgroundImage.size.width, publishButton.currentBackgroundImage.size.height);
  publishButton.center = CGPointMake(self.tabBar.frame.size.width*0.5,self.tabBar.frame.size.height*0.5);
  [self.tabBar addSubview:publishButton];
- 运行结果如图![](images/publishButton.png)
- 点击加号发布按钮,没有反应,因为被其他4个按钮给覆盖住,解决办法为自定义tabBar![](images/fugai.png)
  • 自定义tabBar:
  • New File一个XMGTabBar继承自UITarBar@interface XMGTabBar : UITabBar
  • 更换tarBar

          // 更换tabBar,readonly,无法字节赋值,可以使用KVC
           //  self.tabBar = [[XMGTabBar alloc] init];
          // KVC
          [self setValue:[[XMGTabBar alloc] init] forKeyPath:@"tabBar"];
  • XMGTabBar.m

#import "XMGTabBar.h"

@interface XMGTabBar()
/** 发布按钮 */
@property (nonatomic, weak) UIButton *publishButton;
@end

@implementation XMGTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_icon"] forState:UIControlStateNormal];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"tabBar_publish_click_icon"] forState:UIControlStateHighlighted];
        [self addSubview:publishButton];
        self.publishButton = publishButton;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    // 设置发布按钮的frame
    self.publishButton.bounds = CGRectMake(0, 0, self.publishButton.currentBackgroundImage.size.width, self.publishButton.currentBackgroundImage.size.height);
    self.publishButton.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);

    // 设置其他UITabBarButton的frame
    CGFloat buttonY = 0;
    CGFloat buttonW = self.frame.size.width / 5;
    CGFloat buttonH = self.frame.size.height;
    NSInteger index = 0;
    for (UIView *button in self.subviews) {
//        if (![button isKindOfClass:NSClassFromString(@"UITabBarButton")]) continue;
        if (![button isKindOfClass:[UIControl class]] || button == self.publishButton) continue;

        // 计算按钮的x值
        CGFloat buttonX = buttonW * ((index > 1)?(index + 1):index);
        button.frame = CGRectMake(buttonX, buttonY, buttonW, buttonH);

        // 增加索引
        index++;
    }
}

@end
  • 运行结果如图:

自定义子tabBar的更多相关文章

  1. Matlab定义子函数

    上篇博客介绍了在Matlab中自己定义简单函数的方法,本篇博客将介绍定义子函数的方法.本文承接上篇博客的样例,即随机生成一个3行4列的矩阵,矩阵中的元素设定上下限为(low,high).并返回矩阵全部 ...

  2. 045——VUE中组件之父组件使用scope定义子组件模板结构

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. justify-content 定义子元素在父元素水平位置排列的顺序

    justify-content 定义子元素在父元素水平位置排列的顺序,需要和display:flex使用才会生效. 有五个属性: 1.flex-start(默认值)  左对齐 2.flex-end 右 ...

  4. 45.VUE学习之--组件之父组件使用scope定义子组件模板样式结构实例讲解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器

    一.环境配置,和项目层次搭建 二.自定义TabBar 项目中TabBar中的导航按钮美工给的图片太大,图片中包含了图片和文字.最主要的是TabBar上面的按钮图片尺寸是有规定的,当高度大于44的时候, ...

  6. Java程序员学C#基本语法两个小时搞定(对比学习)

    对于学习一门新的语言,关键是学习新语言和以前掌握的语言的区别,但是也不要让以前语言的东西,固定了自己的思维模式,多看一下新的语言的编程思想. 1.引包 using System;java用import ...

  7. [转] Java程序员学C#基本语法两个小时搞定(对比学习)

    Java程序员学C#基本语法两个小时搞定(对比学习)   对于学习一门新的语言,关键是学习新语言和以前掌握的语言的区别,但是也不要让以前语言的东西,固定了自己的思维模式,多看一下新的语言的编程思想. ...

  8. Spring Boot 返回 XML 数据,一分钟搞定!

    Spring Boot 返回 XML 数据,前提必须已经搭建了 Spring Boot 项目,所以这一块代码就不贴了,可以点击查看之前分享的 Spring Boot 返回 JSON 数据,一分钟搞定! ...

  9. 为Emacs添加标签tabbar功能

    Emacs的强大之处在于,只有你想不到,没有她做不到! 折腾了两个小时,终于在终端putty上搞定了tabbar.下面是一些资源,以方便后面的同学快速搞定. 首先下载tabbar的插件tabbar.e ...

随机推荐

  1. lua元表与元方法

    lua中提供的元表(metatable)与元方法(metamethod)是一种非常重要的语法,metatable主要用于做一些类似于C++重载操作符式的功能. lua中提供的元表是用于帮助lua变量完 ...

  2. 关于type erasure

    哇,好久没有写blog了,再不写的话,blog的秘密都要忘记了,嘿嘿. 最近在试着参与一个开源项目,名字叫avim(A Vibrate IM),别想多了哟.地址是:https://github.com ...

  3. js null 和 undefined

    undefined是一个特殊类型,null本质上是一个对象 typeof undefined//"undefined"typeof null//"object" ...

  4. 《Linux内核分析》期末总结

    Linux内核设计期中总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 前八周博客汇总及总结 Linux内核设计第一周——从汇编语言出发理解计算机工作原理 我们学习了汇编语言的基础知识,这一 ...

  5. android studio 2.0 GPU Debugger使用说明

    GPU Debugger GPU Debugging Tools The GPU debugging tools are an experimental feature intended to hel ...

  6. 《Linux及安全》期中总结&《Linux内核分析》期终总结

    [5216 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000] WEEK NINE ...

  7. golang调用c++文件

    简要步骤: 1,将c++ 的方法提取到头文件.h中( ) 2,编译cc(c++)文件为动态链接库so文件 3,将头文件放入include目录 .so放入lib目录 4,go程序中指定 CFLAGS 和 ...

  8. ruby -检查数据类型

    HashObj={","language"=>"zh","make"=>"Apple"," ...

  9. Go语言的GOPATH与工作目录详解

    这篇文章主要介绍了Go语言的GOPATH与工作目录详解,本文详细讲解了GOPATH设置.应用目录结构.编译应用等内容,需要的朋友可以参考下 GOPATH设置 go 命令依赖一个重要的环境变量:$GOP ...

  10. Python资源大全

    The Python Tutorial (Python 2.7.11) 的中文翻译版本.Python Tutorial 为初学 Python 必备官方教程,本教程适用于 Python 2.7.X 系列 ...