首先看一下效果
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
好了開始正式的操作了
在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è)人分享 如有好的修改地方 請@我