A.搭建基本环境
github:https://github.com/hellovoidworld/HVWWeibo
项目结构:
1.使用代码构建UI,不使用storyboard
AppDelegate:
1- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2//Override point for customization after application launch.34//启动后显示状态栏5UIApplication *app =[UIApplication sharedApplication];6app.statusBarHidden =NO;78//设置window9self.window =[[UIWindow alloc] init];10self.window.frame =[UIScreen mainScreen].bounds;1112[self.window makeKeyAndVisible];1314returnYES;15}
2.使用LaunchImage作为启动图,不使用xib
3.配置图标AppIcon
不使用系统渲染图标
4.设置屏幕方向-->只有竖向
5.启动时隐藏状态栏
B.项目分层 & 创建PCH
1.项目分层
为了让在Finder中显示跟Xcode中显示都是分层效果,首先在Finder中建文件目录层次
再把文件目录拖入Xcode
2.创建并配置一个pch文件,来用声明全局公共宏命令
配置:
C.添加子控制器
1.为每个Tab创建一个集成UITableViewController的类
分别是:首页、信息、发现、我
2.创建一个集成UITabBarController的类作为window的rootViewController
AppDelegate:
1- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2//Override point for customization after application launch.34//启动后显示状态栏5UIApplication *app =[UIApplication sharedApplication];6app.statusBarHidden =NO;78//设置window9self.window =[[UIWindow alloc] init];10self.window.frame =[UIScreen mainScreen].bounds;1112//创建根控制器13HVWTabBarViewController *tabVC =[[HVWTabBarViewController alloc] init];14self.window.rootViewController =tabVC;1516[self.window makeKeyAndVisible];1718returnYES;19}
3.在上述的TabBarController中创建并添加子控件
HVWTabBarViewController.m :
1- (void)viewDidLoad {2[super viewDidLoad];3//Do any additional setup after loading the view.45//添加子控制器6//首页7HVWHomeViewController *homeVC =[[HVWHomeViewController alloc] init];8homeVC.view.backgroundColor =[UIColor redColor];9homeVC.title =@"首页";10[self addChildViewController:homeVC];1112//消息13HVWMessageViewController *messageVC =[[HVWMessageViewController alloc] init];14messageVC.view.backgroundColor =[UIColor blueColor];15messageVC.title =@"消息";16[self addChildViewController:messageVC];1718//发现19HVWDiscoverViewController *discoverVC =[[HVWDiscoverViewController alloc] init];20discoverVC.view.backgroundColor =[UIColor yellowColor];21discoverVC.title =@"发现";22[self addChildViewController:discoverVC];2324//我25HVWPRofileViewController *profileVC =[[HVWProfileViewController alloc] init];26profileVC.view.backgroundColor =[UIColor greenColor];27profileVC.title =@"我";28[self addChildViewController:profileVC];29}
4.为tab添加图片
1.需求:要区分iOS7之前及之后的系统,使用不同的图片
这里创建一个UIImage的分类,新写一个加载图片的的方法,用来自动检测系统版本并加载不同的图片
(1)iOS6使用普通图片, iOS7及以上系统版本使用的是带有"_os7"结尾的图片
(2)添加一条用来判别系统版本的宏
HVWWeibo-Prefix.pch:
1#ifndef HVWWeibo_HVWWeibo_Prefix_pch2#defineHVWWeibo_HVWWeibo_Prefix_pch34//Include any system framework and library headers here that should be included in all compilation units.5//You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.67//判别是否iOS7或以上版本系统8#defineiOS7 ([UIDevice currentDevice].systemVersion.doubleValue >= 7.0)910#endif
(3)创建UIImage+Extension分类
UIImage+Extension.m:
1#import"UIImage+Extension.h"23@implementationUIImage (Extension)45+ (UIImage *) imageWithNamed:(NSString *) imageName {6UIImage *image =nil;78//如果是iOS7或以上版本9if(iOS7) {10image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_os7", imageName]];11}1213//如果是iOS614if(nil ==image) {15image =[UIImage imageNamed:imageName];16}1718returnimage;19}2021@end
(4)添加tab图标
封装一下创建子控制器的代码
HVWTabBarViewController.m:
1#import"HVWTabBarViewController.h"2#import"HVWHomeViewController.h"3#import"HVWMessageViewController.h"4#import"HVWDiscoverViewController.h"5#import"HVWProfileViewController.h"6#import"UIImage+Extension.h"78@interfaceHVWTabBarViewController ()910@end1112@implementationHVWTabBarViewController1314- (void)viewDidLoad {15[super viewDidLoad];16//Do any additional setup after loading the view.1718//添加子控制器19//首页20HVWHomeViewController *homeVC =[[HVWHomeViewController alloc] init];21[self addChildViewController:homeVC WithTitle:@"首页"image:@"tabbar_home"seletectedImage:@"tabbar_home_selected"];2223//消息24HVWMessageViewController *messageVC =[[HVWMessageViewController alloc] init];25[self addChildViewController:messageVC WithTitle:@"消息"image:@"tabbar_message_center"seletectedImage:@"tabbar_message_center_selected"];2627//发现28HVWDiscoverViewController *discoverVC =[[HVWDiscoverViewController alloc] init];29[self addChildViewController:discoverVC WithTitle:@"发现"image:@"tabbar_discover"seletectedImage:@"tabbar_discover_selected"];3031//我32HVWProfileViewController *profileVC =[[HVWProfileViewController alloc] init];33[self addChildViewController:profileVC WithTitle:@"我"image:@"tabbar_profile"seletectedImage:@"tabbar_profile_selected"];3435}3637/** 添加tab子控制器*/38- (void) addChildViewController:(UIViewController *) viewController WithTitle:(NSString *) title image:(NSString *) imageName seletectedImage:(NSString *) selectedImageName {3940//设置随机背景色41viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0green:arc4random_uniform(256)/255.0blue:arc4random_uniform(256)/255.0alpha:1.0];4243//设置标题44viewController.title =title;45//设置图标46viewController.tabBarItem.image =[UIImage imageWithNamed:imageName];4748//被选中时图标49UIImage *selectedImage =[UIImage imageWithNamed:selectedImageName];50//如果是iOS7,不要渲染被选中的tab图标(iOS7中会自动渲染成为蓝色)51if(iOS7) {52selectedImage =[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];53}54viewController.tabBarItem.selectedImage =selectedImage;5556//添加子控制器57[self addChildViewController:viewController];58}5960@end
D.添加导航控制器
1.只是在每个tab的controller上包装了一个UINavigationController
HVWTabBarViewController.m:
1/** 添加tab子控制器*/2- (void) addChildViewController:(UIViewController *) viewController WithTitle:(NSString *) title image:(NSString *) imageName seletectedImage:(NSString *) selectedImageName {34//设置随机背景色5viewController.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0green:arc4random_uniform(256)/255.0blue:arc4random_uniform(256)/255.0alpha:1.0];67//设置标题,直接设置title可以同时设置tabBarItem和navigationItem的title8//viewController.tabBarItem.title = title;9//viewController.navigationItem.title = title;10viewController.title =title;1112//设置图标13viewController.tabBarItem.image =[UIImage imageWithNamed:imageName];1415//被选中时图标16UIImage *selectedImage =[UIImage imageWithNamed:selectedImageName];17//如果是iOS7,不要渲染被选中的tab图标(iOS7中会自动渲染成为蓝色)18if(iOS7) {19selectedImage =[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];20}21viewController.tabBarItem.selectedImage =selectedImage;2223//添加子控制器24UINavigationController *nav =[[UINavigationController alloc] initWithRootViewController:viewController];25[self addChildViewController:nav];26}
2.进入非NavigationController的rootViewController的时候,隐藏底部的TabBar
自定义一个集成UINavigationController的类,代替原来的原生类
重写pushViewController方法,当push的时候隐藏TabBar
#mark:此方法可以作用与所有的非rootViewController,非常好用
HVWNavigationViewController.m:
1#import"HVWNavigationViewController.h"23@interfaceHVWNavigationViewController ()45@end67@implementationHVWNavigationViewController89- (void)viewDidLoad {10[super viewDidLoad];11//Do any additional setup after loading the view.12}1314- (void)didReceiveMemoryWarning {15[super didReceiveMemoryWarning];16//Dispose of any resources that can be recreated.17}1819/** 重写push方法*/20- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {21//如果不是根控制器,隐藏TabBar22if(self.viewControllers.count >0) {23//注意这里不是self(navigationController),是push出来的ViewContoller隐藏TabBar24viewController.hidesBottomBarWhenPushed =YES;25}2627//最后一定要调用父类的方法28[super pushViewController:viewController animated:animated];29}3031@end
E.添加导航栏按钮
需要给各个Tab还有其下的页面添加导航栏按钮
1.在pch文件添加一个随机颜色宏定义和一个debug模式下的log函数
1//HVWWeibo-Prefix.pch2#ifndef HVWWeibo_HVWWeibo_Prefix_pch3#defineHVWWeibo_HVWWeibo_Prefix_pch45//Include any system framework and library headers here that should be included in all compilation units.6//You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.78#ifdef__OBJC__9#import<UIKit/UIKit.h>10#import<Foundation/Foundation.h>11#import"UIImage+Extension.h"12#endif1314//测试用log15#ifdefDEBUG16#defineHVWLog(...) NSLog(__VA_ARGS__)17#else18#defineHVWLog(...)19#endif2021//判别是否iOS7或以上版本系统22#defineiOS7 ([UIDevice currentDevice].systemVersion.doubleValue >= 7.0)2324//随机颜色25#defineRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]262728#endif
使用宏定义的log函数,只有在debug模式下才会转化成为NSLog,release的时候会转为空
1/** 寻找朋友按钮事件*/2- (void) searchFriend {3HVWLog(@"searchFriend");4}
这里可以修改运行模式:
2.创建一个集成UIBarButtonItem的分类,用来创建使用UIButton作为按钮图标的item
1//2//UIBarButtonItem+Extension.m3//HVWWeibo4//5//Created by hellovoidworld on 15/1/31.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"UIBarButtonItem+Extension.h"1011@implementationUIBarButtonItem (Extension)1213+ (instancetype) itemWithImage:(NSString *) imageName hightlightedImage:(NSString *) highlightedImageName target:(id)target selector:(SEL)selector {14UIBarButtonItem *item =[[self alloc] init];1516//创建按钮17UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];18UIImage *image =[UIImage imageNamed:imageName];19[button setImage:image forState:UIControlStateNormal];20[button setImage:[UIImage imageNamed:highlightedImageName] forState:UIControlStateHighlighted];2122//一定要设置frame,才能显示23button.frame = CGRectMake(0,0, image.size.width, image.size.height);2425//设置事件26[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];2728item.customView =button;29returnitem;30}3132@end
3.sample:在“首页”页面加上导航栏按钮
1//HVWHomeViewController.m2- (void)viewDidLoad {3[super viewDidLoad];45//添加导航控制器按钮6//左边按钮7self.navigationItem.leftBarButtonItem = [HVWBarButtonItem itemWithImage:@"navigationbar_friendsearch"hightlightedImage:@"navigationbar_friendsearch_highlighted"target:self selector:@selector(searchFriend)];89//右边按钮10self.navigationItem.rightBarButtonItem = [HVWBarButtonItem itemWithImage:@"navigationbar_pop"hightlightedImage:@"navigationbar_pop_highlighted"target:self selector:@selector(pop)];11}1213/** 左边导航栏按钮事件*/14- (void) searchFriend {15HVWLog(@"searchFriend");16}1718/** 右边导航栏按钮事件*/19- (void) pop {20HVWLog(@"pop");21}
4.给所有非rootViewController加上“返回”按钮和“直接回到rootViewController”按钮
在HVWNavigationViewController的push方法中实现
#mark:由于是在NavigationController中实现,可以一举实现在所有非rootViewController中的效果。
1//HVWNavigationViewController.m2/** 重写push方法*/3- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {4//如果不是根控制器,隐藏TabBar5if(self.viewControllers.count >0) {6//注意这里不是self(navigationController),是push出来的ViewContoller隐藏TabBar7viewController.hidesBottomBarWhenPushed =YES;89//加上“返回上一层”按钮和“直接回到根控制器”按钮10viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_back"hightlightedImage:@"navigationbar_back_highlighted"target:self selector:@selector(back)];1112viewController.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_more"hightlightedImage:@"navigationbar_more_highlighted"target:self selector:@selector(more)];13}1415//最后一定要调用父类的方法16[super pushViewController:viewController animated:animated];17}1819/** 返回上一层*/20- (void) back {21[self popViewControllerAnimated:YES];22}2324/** 返回根控制器*/25- (void) more {26[self popToRootViewControllerAnimated:YES];27}