A.需求
1.模仿“网易彩票”做出有5个导航页面和相应功能的Demo
2.v1.0 版本搭建基本框架
code source:https://github.com/hellovoidworld/HelloLottery
B.搭建基本框架
1.拖入TaBarController,5个NavigationController和对应的5个UIViewController
2.配置图标和启动画面
AppIcon直接拖入图片
LaunchImage在Xcode6中需要先更改启动图使用图库的图片,而不是LaunchImage.xib
2.引入图片包
4. 按照模块分类代码包
3.底部导航--自定义TabBar
(1)基本设计
a.自定义HVWTabBarController、继承自UIView的HVWTabBar、继承UIButton的HVWTabBarButton
b.在自定义TabBar控制器viewDidLoad,删除原来的TabBar,加上自定义的TabBar
c.加入自定义的按钮图片、选中图片
d.选中事件,转换图片
e.按下马上激发按钮激发
重写TabBarButton的[button setHighlighted:]取消高亮状态
HVWTabBarButton:
1//覆盖setHighlighted,取消点击时的高亮状态2- (void)setHighlighted:(BOOL)highlighted {3//[super setHighlighted:highlighted];4}
初步实现都在HVWTabBarController的viewDidLoad方法中:
1- (void)viewDidLoad {2[super viewDidLoad];3//Do any additional setup after loading the view.45//1.删除原来的TabBar6[self.tabBar removeFromSuperview];78//2.添加自定义TabBar9HVWTabBar *hvwTabBar =[[HVWTabBar alloc] init];10hvwTabBar.frame =self.tabBar.frame;11hvwTabBar.backgroundColor = [UIColor greenColor];//设置绿色底的tabBar,稍后会被按钮图片覆盖12[self.view addSubview:hvwTabBar];1314//3.添加按钮15for(inti=0; i<5; i++) {16//3.1创建按钮17HVWTabBarButton *button =[HVWTabBarButton buttonWithType:UIButtonTypeCustom];18button.tag =i;1920//3.2设置按钮背景图片21[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal];2223[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]] forState:UIControlStateSelected];2425//3.3设置frame26CGFloat buttonWidth = hvwTabBar.frame.size.width /5;27CGFloat buttonHeight =hvwTabBar.frame.size.height;28CGFloat buttonX = i *buttonWidth;29CGFloat buttonY =0;30button.frame =CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);3132//3.4添加到tabBar33[hvwTabBar addSubview:button];3435//3.5添加监听事件36[button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];3738//3.6默认已经点击了第一个按钮39if(i ==0) {40[self tabBarButtonClicked:button];41}42}43}4445- (void) tabBarButtonClicked:(HVWTabBarButton *) button {46//1.取消选中之前的按钮47self.selectedButton.selected =NO;4849//2.选中新点击的按钮50button.selected =YES;5152//3.设置为当前选中的按钮53self.selectedButton =button;5455//4.切换子控制器56self.selectedIndex =button.tag;57}
(2)封装TabBar代码
a.重写initWithFrame:创建初始化TabBar
b.移动子控件的初始化代码到layoutSubviews
c.通过代理转换Navigation页面
d.封装添加按钮函数
1//2//HWTabBarController.m3//HelloLottery4//5//Created by hellovoidworld on 14/12/31.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"HvWTabBarController.h"10#import"HVWTabBar.h"11#import"HVWTabBarButton.h"1213@interfaceHVWTabBarController () <HVWTabBarDelegate>1415@end1617@implementationHVWTabBarController1819- (void)viewDidLoad {20[super viewDidLoad];21//Do any additional setup after loading the view.2223//1.删除原来的TabBar24[self.tabBar removeFromSuperview];2526//2.添加自定义TabBar27HVWTabBar *hvwTabBar =[[HVWTabBar alloc] init];28hvwTabBar.frame =self.tabBar.frame;29hvwTabBar.backgroundColor = [UIColor greenColor];//设置绿色底的tabBar,稍后会被按钮图片覆盖30hvwTabBar.delegate=self;31[self.view addSubview:hvwTabBar];32}3334- (void)didReceiveMemoryWarning {35[super didReceiveMemoryWarning];36//Dispose of any resources that can be recreated.37}3839#PRagmamark - HVWTabBarDelegate 代理方法40- (void)hvwTabBar:(HVWTabBar *)hvwTabBar didClickedButtonFrom:(int)from to:(int)to {41//切换子控制器42self.selectedIndex =to;43}4445@end
1//2//HVWTabBar.h3//HelloLottery4//5//Created by hellovoidworld on 14/12/31.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import<UIKit/UIKit.h>1011@classHVWTabBar;1213/** 代理协议*/14@protocolHVWTabBarDelegate <NSObject>15@optional16- (void) hvwTabBar:(HVWTabBar *) hvwTabBar didClickedButtonFrom:(int) from to:(int) to;17@end1819@interfaceHVWTabBar : UIView2021/** 代理*/22@property(nonatomic, weak)id<HVWTabBarDelegate>delegate;2324@end
1//2//HVWTabBar.m3//HelloLottery4//5//Created by hellovoidworld on 14/12/31.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"HVWTabBar.h"10#import"HVWTabBarButton.h"1112#defineHVWTabBarButtonCount 51314@interfaceHVWTabBar()1516@property(nonatomic, weak) HVWTabBarButton *selectedButton;1718@end1920@implementationHVWTabBar2122//重写initWithFrame方法,添加tabBar按钮23-(instancetype)initWithFrame:(CGRect)frame {24if(self =[super initWithFrame:frame]) {25[self initButtons];26}2728returnself;29}3031/** 初始化按钮*/32- (void) initButtons {33for(inti=0; i<HVWTabBarButtonCount; i++) {34//3.1创建按钮35HVWTabBarButton *button =[HVWTabBarButton buttonWithType:UIButtonTypeCustom];36button.tag =i;3738//3.2设置按钮背景图片39[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%d", i+1]] forState:UIControlStateNormal];4041[button setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"TabBar%dSel", i+1]] forState:UIControlStateSelected];4243//3.3添加到tabBar44[self addSubview:button];4546//3.4添加监听事件47[button addTarget:self action:@selector(tabBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];4849//3.5默认已经点击了第一个按钮50if(i ==0) {51[self tabBarButtonClicked:button];52}53}54}5556/** 初始化子控件的位置尺寸*/57- (void)layoutSubviews {58[super layoutSubviews];5960for(inti=0; i<HVWTabBarButtonCount; i++) {61HVWTabBarButton *button =self.subviews[i];62CGFloat buttonWidth = self.frame.size.width /5;63CGFloat buttonHeight =self.frame.size.height;64CGFloat buttonX = i *buttonWidth;65CGFloat buttonY =0;66button.frame =CGRectMake(buttonX, buttonY, buttonWidth, buttonHeight);67}68}6970- (void) tabBarButtonClicked:(HVWTabBarButton *) button {71//1.调用代理方法,通知TabBarController切换子控制器72if([self.delegaterespondsToSelector:@selector(hvwTabBar:didClickedButtonFrom:to:)]) {73[self.delegatehvwTabBar:self didClickedButtonFrom:self.selectedButton.tag to:button.tag];74}7576//2.取消选中之前的按钮77self.selectedButton.selected =NO;7879//3.选中新点击的按钮80button.selected =YES;8182//4.设置为当前选中的按钮83self.selectedButton =button;84}8586@end
4.头部Navigation导航栏主题设置
(1)让applicatoin管理状态栏
(2)Navigation导航栏背景图片
(3)统一设置所有Navigation导航栏
[UINavigationBar appearance]; // 所有Navigation导航栏(头部导航栏)
(4)设置所有Navigation导航栏字体颜色
(5)根据系统版本,设置Navigation导航栏背景图片
(6)在二级页面隐藏底部导航条,重写导航控制器的push方法
a.自定义一个导航控制器HVWNavigationController类,重写push,隐藏底部导航栏
b.设置为每个NavigationController的class
c.导航控制器类的initialize只会在类第一次使用的时候调用一次
所以,导航栏的主题设置可以放在initialize方法中
解决:
(1)使用application管理状态栏
设置不使用控制器控制状态栏
在AppDelegate中设置:
1- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2//Override point for customization after application launch.34//设置状态栏样式为白色5application.statusBarStyle =UIStatusBarStyleLightContent;67returnYES;8}
(2)创建自定义NavigationController类,并设置5个Navigation控制器的class为此类
(3)不要删除原来的tabBar,而是覆盖它,才能控制push事件中底部导航栏
HVWTabBarController:
1- (void)viewDidLoad {2[super viewDidLoad];3//Do any additional setup after loading the view.45//1.添加自定义TabBar6HVWTabBar *hvwTabBar =[[HVWTabBar alloc] init];7hvwTabBar.frame =self.tabBar.bounds;8hvwTabBar.delegate=self;910//2.设置tabBar11[self.tabBar addSubview:hvwTabBar];12}
(4)在NavigationController中编写类初始化方法和重写push方法
1//2//HVWNavigationController.m3//HelloLottery4//5//Created by hellovoidworld on 15/1/1.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"HVWNavigationController.h"101112@interfaceHVWNavigationController ()1314@end1516@implementationHVWNavigationController1718- (void)viewDidLoad {19[super viewDidLoad];20//Do any additional setup after loading the view.2122}2324- (void)didReceiveMemoryWarning {25[super didReceiveMemoryWarning];26//Dispose of any resources that can be recreated.27}2829/** 类初始化方法,仅调用一次*/30+ (void) initialize {31//获取能够控制所有NavigationBar的实例32UINavigationBar *navBar =[UINavigationBar appearance];3334//设置背景图片35NSString *bgImageName;36if(iOS7) {//在HelloLottery-Prefix.pch中定义了判断iOS版本的全局变量37bgImageName =@"NavBar64";38}else{39bgImageName =@"NavBar";40}4142[navBar setBackgroundImage:[UIImage imageNamed:bgImageName] forBarMetrics:UIBarMetricsDefault];4344//设置文本45NSMutableDictionary *attr =[NSMutableDictionary dictionary];46attr[NSForegroundColorAttributeName] =[UIColor whiteColor];47attr[NSFontAttributeName] = [UIFont systemFontOfSize:16];48[navBar setTitleTextAttributes:attr];49}5051//拦截所有的push操作52- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {53viewController.hidesBottomBarWhenPushed = YES;//这是原来NavigationController中的tabBar,所以要设置自定义的tabBar为Navigation中的tabBar54[super pushViewController:viewController animated:YES];55}5657@end58