zoukankan      html  css  js  c++  java
  • 关于iOS开发开心蛙家长iOS端开发过程中的零碎知识归纳(3)---主界面tabBar搭建

    前言:上一个博客中有提到欢迎页面和主界面,说完欢迎页应该来谈谈主界面的搭建了,我目前做的这个项目,符合正常情况上的TabBar+navigation这种。所以接下来就来说一说怎么搭的这个主界面tabBar吧。

    //
    //  QKTabBarController.m
    //  kaixinwaP
    //
    //  Created by wangyao on 15/11/24.
    //  Copyright © 2015年 wangyao. All rights reserved.
    //

    #import "QKTabBarController.h"
    #import "RecommendViewController.h"
    #import "StudyViewController.h"
    #import "QKMeViewController.h"
    #import "QKNavigationController.h"
    @interface QKTabBarController ()

    @end

    @implementation QKTabBarController

    - (void)viewDidLoad {
        [super viewDidLoad];
        
    //    创建三大主视图的控制器
        RecommendViewController * recommendVC =[RecommendViewController new];
        
        StudyViewController * studyVC = [StudyViewController new];
        
        QKMeViewController * meVC = [QKMeViewController new];
        
        
        
        [self addOneChildVC:recommendVC title:@"推荐" imageName:@"tabBar0" selectedImageName:@"tabBar0"];
        [self addOneChildVC:studyVC title:@"学习" imageName:@"tabBar2" selectedImageName:@"tabBar2"];
        [self addOneChildVC:meVC title:@"我" imageName:@"tabBar3" selectedImageName:@"tabBar3"];
        
        
        
        // Do any additional setup after loading the view.
    }
    //手写添加tabBar的方法
    - (void)addOneChildVC:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
    {
        childVC.tabBarItem.image = [UIImage imageNamed:imageName];
        
        
    //    设置控制器的标题为title
        childVC.title = title;
    //    设置tabBarItem的普通文字颜色

        
        
        NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
        textAttrs[NSForegroundColorAttributeName] = QKColor(127, 127, 127);
        textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
        [childVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        
        NSMutableDictionary * selectedTextAttrs = [NSMutableDictionary dictionary];
        selectedTextAttrs[NSForegroundColorAttributeName] = QKColor(45, 201, 45);
        [childVC.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
        
        UIImage * selectedImage = [UIImage imageNamed:selectedImageName];
        if (iOS7) {
            selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            }
        childVC.tabBarItem.selectedImage = selectedImage;
        
    //    添加导航控制器
        QKNavigationController * nav = [[QKNavigationController alloc]initWithRootViewController:childVC];
        [self addChildViewController:nav];
        
        
        
        
        
    }

    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    @end

    总结:

    - (void)addOneChildVC:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
    {
        childVC.tabBarItem.image = [UIImage imageNamed:imageName];
        
        
    //    设置控制器的标题为title
        childVC.title = title;
    //    设置tabBarItem的普通文字颜色

        
        
       1. NSMutableDictionary * textAttrs = [NSMutableDictionary dictionary];
       2. textAttrs[NSForegroundColorAttributeName] = QKColor(127, 127, 127);
       3. textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
       4. [childVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
        
       5. NSMutableDictionary * selectedTextAttrs = [NSMutableDictionary dictionary];
        6.selectedTextAttrs[NSForegroundColorAttributeName] = QKColor(45, 201, 45);
       7. [childVC.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
        
        8.UIImage * selectedImage = [UIImage imageNamed:selectedImageName];
       9. if (iOS7) {
            selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            }
        childVC.tabBarItem.selectedImage = selectedImage;
        
    //    添加导航控制器
        10.QKNavigationController * nav = [[QKNavigationController alloc]initWithRootViewController:childVC];
        [self addChildViewController:nav];
        }
    主要就是这个方法,这个函数里面的大题步骤就是将把传进来的UIViewController设置好各种属性,包括title,图标啊,再给一个一个的加到navigationcontroller中,最后把navigationController设置成tabBar的childViewController

    具体:1~4是由于设置tabBarItem 只能通过一个字典来存属性,所以1~3都是在创建一个,属性的字典,然后在给一个设置,就设置了tabBaritem的字体样式等属性。

    8~9都是给tabBAriTem设置图片

    10.再创建一个navigationcontroller并且以传进来的这个视图控制器为跟视图,最后再把这个带有跟视图的navigationController作为tabbarcontroller的childViewController。

    大功告成!!!

    一句话:当敲代码的速度加快了,脱离贫穷的速度也就快了。

  • 相关阅读:
    Windows系统开机硬盘自检问题解决
    更改软件默认安装路径
    windows添加开机启动项
    xp 如何打开(进行)远程桌面连接
    xp看系统位数
    教你创建一个别人打不开删不掉的文件夹(干坏事爱好者必看)
    无操作一段时间显示器无信号
    长时间用电脑,(给窗口选一个合适的颜色)设置窗口绿色
    Windows右键菜单设置与应用技巧
    Outlook如何定时发邮件
  • 原文地址:https://www.cnblogs.com/YaoWang/p/5048654.html
Copyright © 2011-2022 走看看