首先,一个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. 2019牛客多校B Beauty Values——思维题

    题目 求所有子区间中不同元素之和. 分析 枚举相邻的相同数字形成的区间,计算它是哪些区间的子区间,每次有-1的贡献,将其从总贡献中减去. #include<bits/stdc++.h> u ...

  2. Linux - 网络配置( CentOS 64 )

    终于..今天我终于将linux的网络调试出来了,虽然之前看了一大堆教程,每一个都是一样的步骤,但是,在我这就是弄不好,所以经过不断尝试的我,今天发一个自己配置好的步骤,唉,太痛苦了. - 对了补充一句 ...

  3. Java知识点汇总-1

    目录 1 native方法 2 泛型 3 hashcode 4 JDK主要的包 5 被final修饰的类特点 6 空串与null串 7 判断字符串是否相等 1 native方法 JDK源代码由C++. ...

  4. sql server 数据类型转换

    --这是显示转换类型,将字符串转成整形SELECT CAST('1' AS int),CONVERT(int,'1')--SqlServer也会隐式转换,如:用字符串乘以一个整形,--整形优先级要大, ...

  5. sql server 悲观锁和乐观锁的作用

    sql server对并发的处理-乐观锁和悲观锁 假如两个线程同时修改数据库同一条记录,就会导致后一条记录覆盖前一条,从而引发一些问题. 例如: 一个售票系统有一个余票数,客户端每调用一次出票方法,余 ...

  6. webpack-merge

    配置分离 随着我们业务逻辑的增多,图片.字体.css.ES6以及CSS预处理器和后处理器逐渐的加入到我们的项目中来,进而导致配置文件的增多,使得配置文件书写起来比较繁琐,更严重者(书写特定文件的位置会 ...

  7. ORBSLAM2单目初始化过程

    ORBSLAM2单目初始化过程 转自博客:https://blog.csdn.net/zhubaohua_bupt/article/details/78560966 ORB单目模式的初始化过程可以分为 ...

  8. 走进JavaWeb技术世界14:通过项目逐步深入了解Mybatis(一)

    通过项目逐步深入了解Mybatis(一) 2017-06-12 文章导航 Mybatis 和 SpringMVC 通过订单商品案例驱动 官方中文地址:http://www.mybatis.org/my ...

  9. Hibernate HQL和QBC

    OID查询 一.什么是OID查询 根据对象的OID主键进行检索 二.OID查询方式 1. get方法 当get()方法被调用的时候就会立即发出SQL语句 并且返回的对象也是实际的对象 使用get()和 ...

  10. IIS/VS IIS Express 添加MIME映射 svg、woff、woff2、json

    出现问题 页面提示 font-awesome/fonts/fontawesome-webfont.woff2?v=4.3.0 Failed to load resource: the server r ...