快速搭建一个APP框架(写给新手)

首先看一下效果

快速搭建一个APP框架(写给新手)_第1张图片
Paste_Image.png

步入正题
如何快速搭建一个APP的如图所示的框架呢

创建项目省略 。。。。
1、创建一个Main文件夹 里面放主控制器 1 2 3 4 5 分别代表你的5个功能模块


快速搭建一个APP框架(写给新手)_第2张图片

项目结构如图所示
LLTabBarViewController 需要继承UITabBarController
LLBaseNavigationViewController 需要继承 UINavigationController

快速搭建一个APP框架(写给新手)_第3张图片

好了开始正式的操作了

  1. 在AppDelegate.m内 设置窗口启动根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 1.创建窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

// 2.设置窗口的根控制器
LLTabBarViewController *tabBarController = [[LLTabBarViewController alloc]init];
self.window.rootViewController = tabBarController;

// 3.显示窗口
[self.window makeKeyAndVisible];

return YES;

}
2)在LLTabBarController.m内创建并添加子控制器

首先导入相应的控制器
然后 viewDidLoad下面 实现如下代码
- (void)viewDidLoad
{
[super viewDidLoad];

// 1.初始化子控制器
LLOneViewController *one = [[LLOneViewController alloc] init];
[self addChildVc:one title:@"行情" image:@"TabBar1" selectedImage:@"TabBar1Sel"];

LLTwoViewController *two = [[LLTwoViewController alloc] init];
[self addChildVc:two title:@"自选股" image:@"TabBar2" selectedImage:@"TabBar2Sel"];

LLThreeViewController *three = [[LLThreeViewController alloc] init];
    [self addChildVc:three title:@"我的资产" image:@"TabBar5" selectedImage:@"TabBar5Sel"];

LLFourViewController *four = [[LLFourViewController alloc] init];
[self addChildVc:four title:@"消息" image:@"TabBar4" selectedImage:@"TabBar4Sel"];

LLFiveViewController *five = [[LLFiveViewController alloc] init];
[self addChildVc:five title:@"提醒" image:@"TabBar3" selectedImage:@"TabBar3Sel"];

//默认选择第二个控制器 从 0 开始算
self.selectedIndex = 1;
}

/**
 *  添加一个子控制器
 **  @param childVc       子控制器
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 设置子控制器的文字
childVc.title = title; // 同时设置tabbar和navigationBar的文字
childVc.tabBarItem.title = title;
// 设置子控制器的图片
childVc.tabBarItem.image = [UIImage imageNamed:image];
//声明显示图片的原始式样 不要渲染
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// 先给外面传进来的小控制器 包装 一个导航控制器
HBBaseNavigationController *nav = [[HBBaseNavigationController alloc] initWithRootViewController:childVc];
// 添加为子控制器
[self addChildViewController:nav];
}

到此 我们已经实现了如下效果

快速搭建一个APP框架(写给新手)_第4张图片
Simulator Screen Shot 2015年9月23日 下午6.49.43.png

最后一步就是设置导航控制器的样式了

我们来到
LLBaseNavigationViewController.m 里面

先定义个宏

//Nav颜色
 #define BarThemeColor [UIColor colorWithRed:72/255.0f green:131/255.0f blue:246/255.0f alpha:1

接着 initialize方法
+ (void)initialize
{
// 设置整个项目所有item的主题样式
UIBarButtonItem *item = [UIBarButtonItem appearance];

// 设置普通状态
// key:NS****AttributeName
NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
[item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
[item setTitleTextAttributes:textAttrs forState:UIControlStateHighlighted];

// 设置不可用状态 灰色
NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionary];
disableTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.7];
disableTextAttrs[NSFontAttributeName] = textAttrs[NSFontAttributeName];
[item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];

// 设置导航栏主题
[self setupNavBarTheme];
}

+ (void)setupNavBarTheme
{

//象征控制所有导航栏的外观
//appearance方法返回一个导航栏的外观
UINavigationBar* bar = [UINavigationBar appearance];
//设置导航栏的背景图片
[bar setBackgroundImage:[self createImageWithColor:BarThemeColor] forBarMetrics:UIBarMetricsDefault];
//设置导航栏文字的主题
[bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor whiteColor],NSForegroundColorAttributeName, nil]];

UITabBar *tabBar = [UITabBar appearance];
//设置全局tabBar字体
[tabBar setTintColor:BarThemeColor];
//底部白色
tabBar.backgroundColor = [UIColor whiteColor];

}

#pragma mark 颜色转换为图片
+ (UIImage *)createImageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}

好了 大功告成

如下图

快速搭建一个APP框架(写给新手)_第5张图片
Simulator Screen Shot 2015年9月23日 下午6.59.57.png

个人分享 如有好的修改地方 请@我

你可能感兴趣的:(快速搭建一个APP框架(写给新手))