先看效果图
一般为了实现底部导航和跳转子控制器.一般都是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在新创意的开源框架常用类中可以找到.其他的代码主要都在这里了.都能满足需要了