UITabBarController的使用

Wesley13
• 阅读 542

先看效果图

UITabBarController的使用

一般为了实现底部导航和跳转子控制器.一般都是UITabBarController和UINavigationController嵌套使用

一般将ViewController添加到UINavigationController管理, 然后将UINavigationController添加到UITabBarController,  再将UITabBarController添加到rootViewController .

ViewController -> UINavigationController -> UITabBarController -> rootViewController

其中UITabBarController添加多个UINavigationController或ViewController以实现评级视图的切换.

直接看代码吧,代码有注释

AppDelegate

//
//  AppDelegate.m
//  TabBarTest
//
//  Created by  罗若文 on 2016/11/1.
//  Copyright © 2016年 罗若文. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "NIImage.h"
#import "NITabBarController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //1.创建Window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];

    //a.初始化一个tabBar控制器
    UITabBarController *tb=[[NITabBarController alloc]init];

    //b.创建子控制器
    ViewController *c1=[[ViewController alloc]init];
    //将子控制器添加到UINavigationController中,方便控制器深度的跳转
    UINavigationController * navigationController=[[UINavigationController alloc]initWithRootViewController:c1];
    //设置底部的标题,如果没有设置默认会取控制器的title.  因为c1是在UINavigationController中所以用这样的方式c1.navigationController取到tabBarItem进行设置
    c1.navigationController.tabBarItem.title=@"消息";
    //底部的图片
    c1.navigationController.tabBarItem.image=[NIImage imageNamed:@"app0.png" targetSize:CGSizeMake(50, 50)];
    //设置提示badge
    c1.navigationController.tabBarItem.badgeValue=@"3";

    
    UIViewController *c2=[[UIViewController alloc]init];
    c2.view.backgroundColor=[UIColor brownColor];
    //这个控制器没有添加到UINavigationController中,就可以直接c2.tabBarItem.title设置底部标题
    c2.tabBarItem.title=@"联系人";
    c2.tabBarItem.image=[NIImage imageNamed:@"联系人0.png" targetSize:CGSizeMake(30, 30)];

    UIViewController *c3=[[UIViewController alloc]init];
    c3.tabBarItem.title=@"动态";
    c3.tabBarItem.image=[NIImage imageNamed:@"uncheck.png" targetSize:CGSizeMake(30, 30)];
    //设置底部选中时候的图片
    c3.tabBarItem.selectedImage=[NIImage imageNamed:@"check.png" targetSize:CGSizeMake(30, 30)];
    //修改图片的位置(上,左,下,右)  上和下的要对应偏移,不然会有意向不到的效果,自己试试,  左右也一样
    c3.tabBarItem.imageInsets=UIEdgeInsetsMake(10,-30,-10, 30);
    //修改文字的位置(水平,垂直)
    [c3.tabBarItem setTitlePositionAdjustment:UIOffsetMake(10, -30)];

    UIViewController *c4=[[UIViewController alloc]init];
    c4.tabBarItem.title=@"设置";
    c4.tabBarItem.image=[NIImage imageNamed:@"set0.png" targetSize:CGSizeMake(30, 30)];
    //设置底部选中时候的图片
    c4.tabBarItem.selectedImage=[NIImage imageNamed:@"set1.png" targetSize:CGSizeMake(30, 30)];
    //设置选中图片保持原来图片颜色,未选中也一样设置UIImageRenderingModeAlwaysOriginal
    c4.tabBarItem.selectedImage= [c4.tabBarItem.selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 获取当前类的tabBarItem
    UITabBarItem *BarItem = c4.tabBarItem;
    // 设置所有BarItem的选中时颜色
    // 设置选中文字颜色
    // 创建字典去描述文本
    NSMutableDictionary *attr = [NSMutableDictionary dictionary];
    // 文本颜色 -> 描述富文本属性的key -> NSAttributedString.h
    attr[NSForegroundColorAttributeName] = [UIColor blueColor];
    [BarItem setTitleTextAttributes:attr forState:UIControlStateSelected];
    
    // 通过normal状态设置字体大小
    // 字体大小 跟 normal
    NSMutableDictionary *attrnor = [NSMutableDictionary dictionary];
    // 设置字体
    attrnor[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    [BarItem setTitleTextAttributes:attrnor forState:UIControlStateNormal];
    
    
    //c.添加子控制器到ITabBarController中
    //c.1第一种方式
    //[tb addChildViewController:c1];
    //[tb addChildViewController:c2];

    //c.2第二种方式
    tb.viewControllers=@[navigationController,c2,c3,c4];
    
    //设置标签栏文字和图片的颜色
    tb.tabBar.tintColor = [UIColor orangeColor];
    
    //设置标签栏的颜色
    tb.tabBar.barTintColor = [UIColor blackColor];
    
    //设置标签栏风格(默认高度49)
    tb.tabBar.barStyle = UIBarStyleBlack;
    
    //设置初始状态选中的下标
    tb.selectedIndex = 2;

    //设置选中的背景
    float w=[UIScreen mainScreen].bounds.size.width/4.0;
    tb.tabBar.selectionIndicatorImage=[NIImage imageByScalingToSize:[NIImage imageWithColor:[UIColor greenColor]] targetSize:CGSizeMake(w, 80)];
    
    
    //设置控制器为Window的根控制器
    self.window.rootViewController=tb;
    //2.设置Window为主窗口并显示出来
    [self.window makeKeyAndVisible];
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

如果需要设置tabBar的高度,那么就要重写一下UITabBarController中的viewWillLayoutSubviews方法

//
//  NITabBarController.m
//  TabBarTest
//
//  Created by  罗若文 on 2016/11/1.
//  Copyright © 2016年 罗若文. All rights reserved.
//

#import "NITabBarController.h"

@interface NITabBarController ()

@end

@implementation NITabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //这边重构TabBarController视图
}

/**
 这边重新设置TabBar的高度
 */
- (void)viewWillLayoutSubviews{
    CGRect tabFrame = self.tabBar.frame; //self.TabBar is IBOutlet of your TabBar
    tabFrame.size.height = 80;
    tabFrame.origin.y = self.view.frame.size.height - 80;
    self.tabBar.frame = tabFrame;
}

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

@end

在你用[self.navigationController pushViewController:vc animated:YES];跳转到第二个视图的时候会发现底部的tabBar还在.那么如果不想要显示,就在跳转前设置一下hidesBottomBarWhenPushed属性就行了

ViewController2 * vc=[[ViewController2 alloc]init];
vc.hidesBottomBarWhenPushed=YES;
[self.navigationController pushViewController:vc animated:YES];

这边有引用的NIImage.h在新创意的开源框架常用类中可以找到.其他的代码主要都在这里了.都能满足需要了

http://git.oschina.net/newOriginality/NewIdeasAPI_Base/tree/master/NewIdeasAPI_Base/NewIdeasAPI_Base/NIBaseUtil

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
UITabBarController的简单使用
UITabBarController的使用步骤初始化UITabBarController设置UIWindow的rootViewController为UITabBarController根据具体情况,通过addChildViewController方法添加对应个数的子控制器通常而言,UITabBarController一般作为应用程序的
Easter79 Easter79
3年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
3年前
PHP+jQuery寥寥几行代码轻松实现百度搜索那样的无刷新PJAX的分页列表和导航链接
!(https://static.oschina.net/uploads/space/2016/1208/171419_U00R_561214.png)PHP寥寥几行代码轻松实现百度搜索那样的分页列表和导航链接,某些语言的拥趸哭晕在厕所.<?php$apparray('db_prefix''
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这