首先,一个app的搭建环境非常重要。既要实现基本功能,又要考虑后期优化的性能。

现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理。

新建一个BasicNavigationViewController,继承UINavigationController

在这里实现导航外观,方法什么的。

示例代码如下:

接着自定义一个BasicTabbarViewController,继承UITabBarController

代码如下:

#import <UIKit/UIKit.h>
@class BasicNavgationViewController;

@interface BasicTabbarViewController : UITabBarController

@property (nonatomic, strong) BasicNavgationViewController *homeNavgationController;
@property (nonatomic, strong) BasicNavgationViewController *mineNavgationController;
@property (nonatomic, strong) BasicNavgationViewController *moreNavgationController;

@end

#import "RootViewController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "MoreViewController.h"
#import "BasicNavgationViewController.h"
#define DMNavgationColor DMRGB(65, 109, 218)      //导航颜色

#define DMRGB(r, g, b) [UIColor colorWithRed:(r)/255.f green:(g)/255.f blue:(b)/255.f alpha:1.f]

#define DMTableViewGrayColor DMRGB(245, 245, 249) // 亮灰(tableView背景)
@interface BasicTabbarViewController ()<UITabBarControllerDelegate>

@end

@implementation BasicTabbarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setCustomAppearance];
    [self setTabBarController];

}

#pragma mark - 设置TabBarController
- (void)setTabBarController
{
    self.delegate = self;
    [self setViewControllers:[self viewControllers]];
    }

- (NSArray *)viewControllers
{
    HomeViewController *homeVC=[[HomeViewController alloc]init];
    self.homeNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:homeVC];

_homeNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"btn_首页_none"] selectedImage:[UIImage imageNamed:@"btn_首页_selected"]];
    _homeNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_首页_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _homeNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeHome;

MineViewController *mineVC=[[MineViewController alloc]init];
    self.mineNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:mineVC];
    _mineNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"btn_我的_none"] selectedImage:[UIImage imageNamed:@"btn_我的_selected"]];
    _mineNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_我的_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _mineNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeMine;

MoreViewController *moreVC=[[MoreViewController alloc]init];
    self.moreNavgationController = [[BasicNavgationViewController alloc]initWithRootViewController:moreVC];
_moreNavgationController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"更多" image:[UIImage imageNamed:@"btn_更多_none"] selectedImage:[UIImage imageNamed:@"btn_更多_selected"]]
    ;
    _moreNavgationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"btn_更多_selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    _moreNavgationController.tabBarItem.tag = DMLoginSuccessBackTypeMore;

return @[_homeNavgationController, _mineNavgationController, _moreNavgationController];

//    NSArray *controllers = [NSArray arrayWithObjects:_BoutiqueGoodsNavgationController,_CategoryNavgationController,_ShopNavgationController,_orderNavgationController,_MyNavgationController,nil];
//    
//    self.viewControllers = controllers;  也可以这样代替
}

#pragma mark - 自定义控件外观
- (void)setCustomAppearance
{
    /* UINavigationBar */
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    NSDictionary *nNormalDictionary = @{NSForegroundColorAttributeName: [UIColor whiteColor],
                                        NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f]};
    [[UINavigationBar appearance] setTitleTextAttributes:nNormalDictionary];
    //    [[UINavigationBar appearance] setTranslucent:NO];
    [[UINavigationBar appearance] setBarTintColor:DMNavgationColor];
    
    /* UITarBar */
    //    [[UITabBar appearance] setTintColor:[UIColor whiteColor]];
    
    /* UITarBarItem */
    // 设置正常状态下TabBarItem字体
    NSDictionary *normalDictionary = @{NSForegroundColorAttributeName: DMRGB(143, 151, 175),
                                       NSFontAttributeName:[UIFont systemFontOfSize:10.0f]};
    [[UITabBarItem appearance] setTitleTextAttributes:normalDictionary forState:UIControlStateNormal];
    // 设置选中状态下TabBarItem字体
    NSDictionary *selectedDictionary = @{NSForegroundColorAttributeName: DMRGB(68, 112, 224),
                                         NSFontAttributeName:[UIFont systemFontOfSize:10.0f]};
    [[UITabBarItem appearance] setTitleTextAttributes:selectedDictionary forState:UIControlStateSelected];
    [[UITabBar appearance]setBackgroundColor:DMTableViewGrayColor];
    [self.tabBar setClipsToBounds:YES];
}

#pragma mark - UITabBarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{

return YES;

}

最后:自定义一个RootviewController,继承UIViewController,以后从控制器创建都继承于它即可。比如:login控制器,register控制器,HomeViewController,mineViewController,MoreViewController等等等等。

iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)的更多相关文章

  1. app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)

    首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...

  2. 重写系统自带tabbar出现的 代理错误

  3. 微信小程序自定义导航栏

    微信小程序需要自定义导航栏,特别是左上角的自定义设置,可以设置返回按钮,菜单按钮,配置如下: 1.在app.json的window属性中增加: navigationStyle:custom 顶部导航栏 ...

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

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

  5. AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController

    AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...

  6. IOS开发之——自定义导航控制器

    BasicNavigationViewController:UINavigationViwController /* 隐藏导航底部线条 */ -(void)viewDidLoad{    [super ...

  7. iOS:视图切换的第二种方式:UINavigationController导航栏控制器

    UINavigationController:一个以栈的形式管理多视图的容器,负责子控制器之间的跳转.由于以栈的方式管理视图,各个视图的切换就是压栈和出栈操作,所以出栈后的视图会立即销毁. 介绍: & ...

  8. iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期

    iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期 一.基本过程 新建一个项目,系统默认的主控制器继承自UIViewController,把主控制器两个文件删掉. 在stor ...

  9. iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决

    我相信针对每一个iOS开发者来说~除了根视图控制器外~所有的界面通过导航栏push过去的界面都是可以通过右滑来返回上一个界面~其实~在很多应用和APP中~用户已经习惯了这个功能~然而~作为开发者的我们 ...

随机推荐

  1. 对比AngularJS/jQueryUI/Extjs:没有一个框架是万能的

    AngularJS不能做什么?对比Angular/JSjQueryUI/Extjs 框架就好比兵器,你得明白你手里拿的是屠龙刀还是倚天剑,刀法主要是砍,剑法主要是刺.对于那些职业喷子和脑残粉,小僧送你 ...

  2. BZOJ 3749: [POI2015]Łasuchy(贪心)

    Orz大佬博客 CODE #include <bits/stdc++.h> using namespace std; typedef long long LL; char cb[1< ...

  3. SCSS 教程

    https://www.jianshu.com/p/a99764ff3c41 https://www.sass.hk/guide/ 1. 使用变量; sass让人们受益的一个重要特性就是它为css引入 ...

  4. javascript权威指南第20章 JSON

    //20.1 语法 //JAVASCRIPT 是对JSON数据支持的. //JSON 可以申明三种类型的值 简单值("hello world") 对象({"name&qu ...

  5. Generator(生成器)函数

    一.基础知识 Generator函数是ES6出现的一种异步操作实现方案. 异步即代码分两段,但是不是连续执行,第一段执行完后,去执行其他代码,等条件允许,再执行第二段. 同步即代码连续执行. 1. G ...

  6. 最短路--Bellman-Ford

    Bellman-Ford 贝尔曼-福特 算法思想 贝尔曼-福特算法(英语:Bellman–Ford algorithm),求解单源最短路径问题的一种算法,由理查德·贝尔曼 和 莱斯特·福特 创立的.它 ...

  7. PHP+下载文件夹

    php下载文件我整理了这三种方法,和大家分享一下: 第一种:直接添加文件下载的绝对路径连接 //如:我有一个文件在demo.xx.cn/demo.zip<button>    <a ...

  8. 10月清北学堂培训 Day 1

    今天是杨溢鑫老师的讲授~ T1 1 题意: n * m 的地图,有 4 种不同的地形(包括空地),6 种不同的指令,求从起点及初始的状态开始根据指令行动的结果. 2 思路:(虽然分了数据范围但是实际上 ...

  9. 牛客训练21674——牛牛与LCM

    Problem 链接:https://ac.nowcoder.com/acm/problem/21674 来源:牛客网 牛牛最近在学习初等数论,他的数学老师给他出了一道题,他觉得太简单了, 懒得做,于 ...

  10. Java基础系列 - 子类继承父类,调用父类的构造函数

    package com.test7; public class test7 { public static void main(String[] args) { Son son = new Son(1 ...