app整体搭建环境: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等等等等。
app整体搭建环境:tabBar切换不同控制器的封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)的更多相关文章
- iOS-tabBar切换不同控制器封装(自定义导航+自定义uiviewcontroler+系统自带tabbar+自定义tabbarController)
首先,一个app的搭建环境非常重要.既要实现基本功能,又要考虑后期优化的性能. 现在很多应用不仅仅是系统自带的控制器,由于需求复杂,基本上需要自定义多控制器来管理. 新建一个BasicNavigati ...
- 重写系统自带tabbar出现的 代理错误
- Android app内语言环境切换
逻辑很简单: 1 app内所有activity继承自BaseActivity或BaseActivity派生出来的子类,BaseActivity中维护了一个静态的 app Activity访问栈,在创 ...
- iOS彩票项目--第一天,自定义TabBar控制器和自定义TabBar,自定义导航控制器
一.环境配置,和项目层次搭建 二.自定义TabBar 项目中TabBar中的导航按钮美工给的图片太大,图片中包含了图片和文字.最主要的是TabBar上面的按钮图片尺寸是有规定的,当高度大于44的时候, ...
- [转]Android App整体架构设计的思考
1. 架构设计的目的 对程序进行架构设计的原因,归根到底是为了提高生产力.通过设计使程序模块化,做到模块内部的高聚合和模块之间的低耦合.这样做的好处是使得程序在开发的过程中,开发人员只需要专注于一点, ...
- Xamarin 跨移动端开发系列(01) -- 搭建环境、编译、调试、部署、运行
如果是.NET开发人员,想学习手机应用开发(Android和iOS),Xamarin 无疑是最好的选择,编写一次,即可发布到Android和iOS平台,真是利器中的利器啊!好了,废话不多说,就开始吧, ...
- Java基础笔记(1) 语言 JAVA的历史 Java的搭建环境
本文除了搭建是重点,其他的都当阅读小说一样去看就好了,不想看可以直接抓住重点,我会改变颜色勾出重点! 英语是人与人交流沟通的重要方式之一.JAVA:是人与计算机沟通交流重要方式之一.我们除了用java ...
- [Laravel] mac下通过 homestead 搭建环境 到运行项目
seven_Android 关注 2017.07.03 21:33* 字数 2240 阅读 3464评论 10喜欢 9 之前学习过一段时间的 Laravel ,换 mac 后一直没空做相关的事情,而且 ...
- AngularJS搭建环境
一.搭建环境 1.1 调试工具:batarang Chrome浏览器插件 主要功能:查看作用域.输出高度信息.性能监控 1.2 依赖软件:Node.js 下载:https://nodejs.org/e ...
随机推荐
- 【mysql】关于临时表
mysql官方的介绍 In some cases, the server creates internal temporary tables while processing queries. Suc ...
- HDU 4043 FXTZ II (组合数学-排列组合)
FXTZ II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- javascript 特效实现(3)—— 鼠标滑过显示二级菜单效果
1. 关键代码:使用 switch 或 if 判断语句,改变对应的二级菜单显示方式为 block 或 none function selectTabMenu(i){ switch(i){ case 7 ...
- python django 模板
1 用两个大括号括起来的文字{{person_name}} 称为变量 2 被 大括号和面分号包围的文件({% if ordered_warranty %})是模板标签 3 过滤器是用管道符(|) 和U ...
- textarea文本简单样式编辑
第一种方法采用替换:就是将文本域的换号符号\r\n,替换成其他符号,存入数据库,然后显示的时候再转换回来: //转换换行符$str=preg_replace("/\r\n|\r|\n/&qu ...
- Mathout
1. 用Maven搭建Mahout的开发环境,并完成PPT 26页,最简单的例子.要求有过程说明和截图. 2. 用案例的数据集,基于Mahout,任选一种算法,对任意一个女性用户进行协同过滤推荐,并解 ...
- Half Wavelength Dipole Antenna
Reference : 1. wikipedia The dipole antenna is the simplest and most widely used class of antenna.It ...
- uva 1572 self-assembly ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxQAAANxCAYAAAB9uv94AAAgAElEQVR4nOxdPW7tOpLWFrQGJb72vI ...
- 2014 Super Training #4 G What day is that day? --两种方法
原题: ZOJ 3785 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3785 题意:当天是星期六,问经过1^1+2^2+ ...
- Topcoder SRM 619 DIv2 500 --又是耻辱的一题
这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <c ...