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

首先看一下效果

Paste_Image.png

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

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


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

好了开始正式的操作了

  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];
}

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

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;
}

好了 大功告成

如下图

Simulator Screen Shot 2015年9月23日 下午6.59.57.png

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文链接http://www.cnblogs.com/kenshincui/p/4186022.html 音频在i...
    Hyman0819阅读 21,785评论 4 74
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,859评论 18 139
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,551评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,141评论 5 13
  • 任何事任何人都能放下,放不下的只是你的内心。没有谁能阻止你成功,前提是你想成功。 这几天想了很多,冲动...
    不二的哥阅读 275评论 3 2