快速搭建一個(gè)APP框架(寫給新手)

首先看一下效果

Paste_Image.png

步入正題
如何快速搭建一個(gè)APP的如圖所示的框架呢

創(chuàng)建項(xiàng)目省略 。。。。
1、創(chuàng)建一個(gè)Main文件夾 里面放主控制器 1 2 3 4 5 分別代表你的5個(gè)功能模塊


項(xiàng)目結(jié)構(gòu)如圖所示
LLTabBarViewController 需要繼承UITabBarController
LLBaseNavigationViewController 需要繼承 UINavigationController

好了開始正式的操作了

  1. 在AppDelegate.m內(nèi) 設(shè)置窗口啟動根控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

// 1.創(chuàng)建窗口
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

// 2.設(shè)置窗口的根控制器
LLTabBarViewController *tabBarController = [[LLTabBarViewController alloc]init];
self.window.rootViewController = tabBarController;

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

return YES;

}
2)在LLTabBarController.m內(nèi)創(chuàng)建并添加子控制器

首先導(dǎo)入相應(yīng)的控制器
然后 viewDidLoad下面 實(shí)現(xiàn)如下代碼
- (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:@"我的資產(chǎn)" 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"];

//默認(rèn)選擇第二個(gè)控制器 從 0 開始算
self.selectedIndex = 1;
}

/**
 *  添加一個(gè)子控制器
 **  @param childVc       子控制器
 *  @param title         標(biāo)題
 *  @param image         圖片
 *  @param selectedImage 選中的圖片
 */
- (void)addChildVc:(UIViewController *)childVc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
// 設(shè)置子控制器的文字
childVc.title = title; // 同時(shí)設(shè)置tabbar和navigationBar的文字
childVc.tabBarItem.title = title;
// 設(shè)置子控制器的圖片
childVc.tabBarItem.image = [UIImage imageNamed:image];
//聲明顯示圖片的原始式樣 不要渲染
childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

// 先給外面?zhèn)鬟M(jìn)來的小控制器 包裝 一個(gè)導(dǎo)航控制器
HBBaseNavigationController *nav = [[HBBaseNavigationController alloc] initWithRootViewController:childVc];
// 添加為子控制器
[self addChildViewController:nav];
}

到此 我們已經(jīng)實(shí)現(xiàn)了如下效果

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

最后一步就是設(shè)置導(dǎo)航控制器的樣式了

我們來到
LLBaseNavigationViewController.m 里面

先定義個(gè)宏

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

接著 initialize方法
+ (void)initialize
{
// 設(shè)置整個(gè)項(xiàng)目所有item的主題樣式
UIBarButtonItem *item = [UIBarButtonItem appearance];

// 設(shè)置普通狀態(tài)
// 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];

// 設(shè)置不可用狀態(tài) 灰色
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];

// 設(shè)置導(dǎo)航欄主題
[self setupNavBarTheme];
}

+ (void)setupNavBarTheme
{

//象征控制所有導(dǎo)航欄的外觀
//appearance方法返回一個(gè)導(dǎo)航欄的外觀
UINavigationBar* bar = [UINavigationBar appearance];
//設(shè)置導(dǎo)航欄的背景圖片
[bar setBackgroundImage:[self createImageWithColor:BarThemeColor] forBarMetrics:UIBarMetricsDefault];
//設(shè)置導(dǎo)航欄文字的主題
[bar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                             [UIColor whiteColor],NSForegroundColorAttributeName, nil]];

UITabBar *tabBar = [UITabBar appearance];
//設(shè)置全局tabBar字體
[tabBar setTintColor:BarThemeColor];
//底部白色
tabBar.backgroundColor = [UIColor whiteColor];

}

#pragma mark 顏色轉(zhuǎn)換為圖片
+ (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

個(gè)人分享 如有好的修改地方 請@我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 原文鏈接http://www.cnblogs.com/kenshincui/p/4186022.html 音頻在i...
    Hyman0819閱讀 21,791評論 4 74
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,915評論 18 139
  • 在iOS中隨處都可以看到絢麗的動畫效果,實(shí)現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺ios動畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,566評論 6 30
  • 在iOS中隨處都可以看到絢麗的動畫效果,實(shí)現(xiàn)這些動畫的過程并不復(fù)雜,今天將帶大家一窺iOS動畫全貌。在這里你可以看...
    F麥子閱讀 5,141評論 5 13
  • 任何事任何人都能放下,放不下的只是你的內(nèi)心。沒有誰能阻止你成功,前提是你想成功。 這幾天想了很多,沖動...
    不二的哥閱讀 279評論 3 2