iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个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)的更多相关文章
- app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...
- 重写系统自带tabbar出现的 代理错误
- 微信小程序自定义导航栏
微信小程序需要自定义导航栏,特别是左上角的自定义设置,可以设置返回按钮,菜单按钮,配置如下: 1.在app.json的window属性中增加: navigationStyle:custom 顶部导航栏 ...
- iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器
一.环境配置,和项目层次搭建 二.自定义TabBar 项目中TabBar中的导航按钮美工给的图片太大,图片中包含了图片和文字.最主要的是TabBar上面的按钮图片尺寸是有规定的,当高度大于44的时候, ...
- AJ学IOS 之微博项目实战(2)微博主框架-自定义导航控制器NavigationController
AJ分享,必须精品 一:添加导航控制器 上一篇博客完成了对底部的TabBar的设置,这一章我们完成自定义导航控制器(NYNavigationController). 为啥要做自定义呢,因为为了更好地封 ...
- IOS开发之——自定义导航控制器
BasicNavigationViewController:UINavigationViwController /* 隐藏导航底部线条 */ -(void)viewDidLoad{ [super ...
- iOS:视图切换的第二种方式:UINavigationController导航栏控制器
UINavigationController:一个以栈的形式管理多视图的容器,负责子控制器之间的跳转.由于以栈的方式管理视图,各个视图的切换就是压栈和出栈操作,所以出栈后的视图会立即销毁. 介绍: & ...
- iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期
iOS开发UI篇—使用storyboard创建导航控制器以及控制器的生命周期 一.基本过程 新建一个项目,系统默认的主控制器继承自UIViewController,把主控制器两个文件删掉. 在stor ...
- iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决
我相信针对每一个iOS开发者来说~除了根视图控制器外~所有的界面通过导航栏push过去的界面都是可以通过右滑来返回上一个界面~其实~在很多应用和APP中~用户已经习惯了这个功能~然而~作为开发者的我们 ...
随机推荐
- 原生js手机端触摸下拉刷新
废话不多说,直接上代码,这里感谢我的好朋友,豆姐 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- java.sql.SQLException: connection holder is null;
一.问题来源分析 出现的错误 : Cause: java.sql.SQLException: connection holder is null; uncategorized SQLException ...
- 学到了林海峰,武沛齐讲的Day26 反射 组合的方式完成授
class BlackMedium: feature='Ugly' def __init__(self,name,addr): self.name=name self.addr=addr def se ...
- 使用webuploader实现大文件传输
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- (转)实验文档5:企业级kubernetes容器云自动化运维平台
部署对象式存储minio 运维主机HDSS7-200.host.com上: 准备docker镜像 镜像下载地址 复制 12345678910111213141516 [root@hdss7-200 ~ ...
- 模糊查询(附上源码和jquery-1.12.1.js,jquery-ui.js,jquery-ui.css)
直接上源码: <!doctype html> <html lang="en"> <head> <meta charset="ut ...
- Go开发环境安装与环境变量配置
1.Go安装包下载 https://studygolang.com/dl 2.安装 Windows平台下,直接下一步即可完成安装. 3.配置环境变量 系统变量 安装完成后,在系统变量的Path一栏,会 ...
- 搜索sqlserver 存储过程中的关键字
搜索sqlserver 存储过程中的关键字 select * from sys.all_sql_modules where definition like '%SP_NAME%'
- Js 之生成二维码插件(jquery.qrcode.js)
一.下载 链接:https://pan.baidu.com/s/1cMjaCYQ_buZNT5XRRjuNTA提取码:myqm 二.效果图 三.代码 <!DOCTYPE html> < ...
- Docker实战笔记
好记性不如烂笔头,持续高产~ 0x01 Docker创建nginx容器 1.流程 docker info 显示Docker系统信息,包括镜像和容器数 示例: 使用镜像 nginx:latest 以交互 ...