很多时候, 我们会发现主流的App设置趋于同质化. 那么在繁杂的开发过程中, 如何来快速搭建一个主流的App框架, 可以大大节省开发的时间. 下面, 我就来帮助大家来快速做出一个App主流框架.
I. AppDelegate
#import "AppDelegate.h"
#import "QMTabBarController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//这个方法里面做设置
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/**在App启动的时候, 自定义一些内容*/
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
//调用自己创建的一个继承自UITabBarController的类,进行初始化
QMTabBarController *tabBarController = [[QMTabBarController alloc] init];
//把Window的根视图控制器设置为该tabBarController
self.window.rootViewController = tabBarController;
//把该window设置为KeyWindow, 并且显示出来
[self.window makeKeyAndVisible];
return YES;
}
II. 自定义TableViewController
#import "QMTabBarController.h"
#improt "QMTestViewController"
#import "QMTabBar.h"
#define kTabBarItemTitleFontSize 10 //tabBarItem的字体大小
@implementation QMTabBarController
+ (void)initialize
{
//普通状态文字设置
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
attrs[NSFontAttributeName] = [UIFont systemFontOfSize:kTabBarItemTitleFontSize];
//选中状态文字设置
NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
selectedAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
selectedAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:kTabBarItemTitleFontSize];
//通过tabBarItem的Appearance统一设置所有tabBarItem的文字属性. 后面带有UI_APPEARANCE_SELECTOR, 都可以通过Appearance对象来统一设置
UITabBarItem *tabBarItem = [UITabBarItem appearance];
//tabBarItem普通状态文字设置
[tabBarItem setTitleTextAttributes:attrs forState:UIControlStateNormal];
//tabBarItem选中状态文字设置
[tabBarItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
}
- (void)viewDidLoad {
[super viewDidLoad];
//测试样例(创建4个自定义控制器)
[self setupChildVC:[[QMTestViewController alloc] init] title:@"测试1" image:@"tabBar_test1_icon" selectedImage:@"tabBar_test1_click_icon"];
[self setupChildVC:[[QMTestViewController alloc] init] title:@"测试2" image:@"tabBar_test2_icon" selectedImage:@"tabBar_test2_click_icon"];
[self setupChildVC:[[QMTestViewController alloc] init] title:@"测试3" image:@"tabBar_test3_icon" selectedImage:@"tabBar_test3_click_icon"];
[self setupChildVC:[[QMTestViewController alloc] init] title:@"测试4" image:@"tabBar_test4_icon" selectedImage:@"tabBar_test4_click_icon"];
//实现自定义TabBar样式, 并且利用KVC,绕过只读属性, 给成员变量赋值
[self setValue:[[QMTabBar alloc] init] forKeyPath:@"tabBar"];
}
/**
* 初始化子控制器
*
* @param vc 子控制器的类型
* @param title tabBarItem的标题
* @param image 未选中图片
* @param selectedImage 选中图片
*/
- (void)setupChildVC:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
//设置导航栏标题
vc.navigationItem.title = title;
//设置底部tabBar标题
vc.tabBarItem.title = title;
//设置底部tabBar的未选中图片
vc.tabBarItem.image = [UIImage imageNamed:image];
//设置底部tabBar的选中图片, 并且图片不进行渲染
UIImage *nonRenderImage = [UIImage imageNamed:selectedImage];
nonRenderImage = [nonRenderImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
vc.tabBarItem.selectedImage = nonRenderImage;
//包装一个自定义导航控制器, 添加导航控制器为QMTabBarController的子控制器
QMNavigationController *nav = [[QMNavigationController alloc] initWithRootViewController:vc];
[self addChildViewController:nav];
}
这里只是把如何创建’TabBarViewController’这部分简单的介绍一下. 如何自定义TabBar, 以及自定义NavigationController, 大家可以根据实际需求自行设计.