A.需求
1.优化项目设置
2.自定义导航栏标题按钮
3.多版本处理
4.iOS6和iOS7的适配
5.设置按钮背景
6.设置值UIBarButtonItem样式
code source: https://github.com/hellovoidworld/HelloLottery
B.实现
1.项目配置
(1)程序启动期间隐藏状态栏
(2)程序启动完成显示状态栏
AppDelegate:
1- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {2//Override point for customization after application launch.34//设置状态栏样式为白色5application.statusBarHidden =NO;6application.statusBarStyle =UIStatusBarStyleLightContent;78returnYES;9}
(3)取消渲染app图标(取消系统渲染效果)
2.自定义导航栏标题按钮
“购彩大厅”右上角的“咨询”按钮是图标+文字型的
(1)UIBarButtonItem在storyboard中只能选择文字或者图片之一(注意NavigationBar内只能放UIBarButtonItem)
(2)UIBarButtonItem可以包含其他控件,这里我们使用UIButton
(3)设置宽度、内间距
3.“合买跟单”导航栏主题点击下拉菜单
(1)使用UIButton作为title item
(2)自定义UIButton,交换按钮title和image的位置,实现titleRectForContentRect和imageRectForContentRect,控制内部控件的frame
1//2//TitleExtendButton.m3//HelloLottery4//5//Created by hellovoidworld on 15/1/3.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"TitleExtendButton.h"1011@interfaceTitleExtendButton()1213@PRoperty(nonatomic, strong) UIFont *titleFont;1415@end1617@implementationTitleExtendButton1819/** 从文件加载对象就会调用此方法,例如xib和storybard*/20- (id)initWithCoder:(NSCoder *)aDecoder {21NSLog(@"从文件加载TitleButton");22if(self =[super initWithCoder:aDecoder]) {23[self initializeButton];24}2526returnself;27}2829/** 从代码中加载对象就会调用此方法*/30-(instancetype)initWithFrame:(CGRect)frame {31NSLog(@"从代码加载TitleButton");32if(self =[super initWithFrame:frame]) {33[self initializeButton];34}3536returnself;37}3839- (void) initializeButton {40//设置font41self.titleFont = [UIFont systemFontOfSize:14];//暂时先自定义font42self.titleLabel.font =self.titleFont;4344//图标居中45[self.imageView setContentMode:UIViewContentModeCenter];46}474849/** 返回title的frame*/50-(CGRect)titleRectForContentRect:(CGRect)contentRect {51CGFloat titleX =0;52CGFloat titleY =0;5354NSDictionary *attr =@{NSFontAttributeName : self.titleFont};55CGFloat titleWidth;5657//只有 iOS7 版本以上才能运行以下代码58if(iOS7) {59//只有 Xcode5 或以上版本才能识别这段代码60#ifdef__ipHONE_7_061titleWidth =[self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil].size.width;62#else63titleWidth =[self.currentTitle sizeWithFont:self.titleFont].width;64#endif65}else{//否则使用旧的API66titleWidth =[self.currentTitle sizeWithFont:self.titleFont].width;67}6869CGFloat titleHeight =contentRect.size.height;7071returnCGRectMake(titleX, titleY, titleWidth, titleHeight);72}7374/** 返回image的frame*/75-(CGRect)imageRectForContentRect:(CGRect)contentRect {76CGFloat imageWidth =30;77CGFloat imageHeight =contentRect.size.height;78CGFloat imageX = contentRect.size.width -imageWidth;79CGFloat imageY =0;80returnCGRectMake(imageX, imageY, imageWidth, imageHeight);81}8283@end
(3)自定义一个集成UIViewController的类,设置为该页面的控制器类,拖线监听title item点击事件
a.按钮效果(“三角形”下拉效果旋转)
b.创建一个UIView,点击下拉显示,再次点击隐藏
1//2//HVWBuyViewController.m3//HelloLottery4//5//Created by hellovoidworld on 15/1/3.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"HVWBuyViewController.h"10#import"TitleExtendButton.h"1112@interfaceHVWBuyViewController ()1314@property(nonatomic, weak) UIView *popupView;1516/** 标题点击事件*/17- (IBAction)titleClicked:(TitleExtendButton *)sender;1819@end2021@implementationHVWBuyViewController2223- (void)viewDidLoad {24[super viewDidLoad];25//Do any additional setup after loading the view.2627}2829- (void)didReceiveMemoryWarning {30[super didReceiveMemoryWarning];31//Dispose of any resources that can be recreated.32}3334/** 延迟初始化弹出view35* (发现放在viewDidLoad的时候,在点击按钮调用的时候pupupView的frame没有被初始化)36*/37- (UIView *)popupView {38if(_popupView ==nil) {39//初始化弹出view40UIView *popupView =[[UIView alloc] init];41CGFloat popupViewX =0;42CGFloat popupViewY = [UIApplication sharedApplication].statusBarFrame.size.height +self.navigationController.navigationBar.frame.size.height;43CGFloat popupViewWidth =self.navigationController.navigationBar.frame.size.width;44CGFloat popupViewHeight = self.view.frame.size.height - popupViewY -self.tabBarController.tabBar.frame.size.height;45popupView.frame =CGRectMake(popupViewX, popupViewY, popupViewWidth, popupViewHeight);46popupView.backgroundColor =[UIColor grayColor];4748UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,10,200,100)];49label.text =@"这是全部彩种de弹出内容";50[popupView addSubview:label];5152self.popupView =popupView;53NSLog(@"%@", NSStringFromCGRect(self.popupView.frame));54}5556return_popupView;57}5859/** 标题点击事件60* 转换箭头方向61* 伸缩内容62*/63- (IBAction)titleClicked:(TitleExtendButton *)sender {64[UIView animateWithDuration:0.5animations:^{65//按钮旋转66sender.imageView.transform =CGAffineTransformRotate(sender.imageView.transform, M_PI);67}];6869//弹出view70if(![self.popupView isDescendantOfView:self.view]) {71[self.view addSubview:self.popupView];72}else{73[self.popupView removeFromSuperview];74}75}76@end
4.view的初始化方法
(1)awakeFromNib和initWithCoder:差别
awakeFromNib 从xib或者storyboard加载完毕就会调用
initWithCoder: 只要对象是从文件解析来的,就会调用
同时存在会先调用initWithCoder:
(2)initWithCoder: & initWithFrame:
initWithCoder:使用文件加载的对象调用(如从xib或stroyboard中创建)
initWithFrame:使用代码加载的对象调用(使用纯代码创建)
注意:所以为了同时兼顾从文件和从代码解析的对象初始化,要同时在initWithCoder: 和 initWithFrame: 中进行初始化
5.使用不同版本Xcode编译代码的时候,进行适配
#import <Availability.h> 定义了SDK版本的宏
1#define__IPHONE_2_0 200002#define__IPHONE_2_1 201003#define__IPHONE_2_2 202004#define__IPHONE_3_0 300005#define__IPHONE_3_1 301006#define__IPHONE_3_2 302007#define__IPHONE_4_0 400008#define__IPHONE_4_1 401009#define__IPHONE_4_2 4020010#define__IPHONE_4_3 4030011#define__IPHONE_5_0 5000012#define__IPHONE_5_1 5010013#define__IPHONE_6_0 6000014#define__IPHONE_6_1 6010015#define__IPHONE_7_0 7000016#define__IPHONE_7_1 7010017#define__IPHONE_8_0 8000018#define__IPHONE_8_1 80100
6.iOS6 和 iOS7 的简单适配
由于iOS6及之前的版本,屏幕view的位置尺寸是需要去掉状态栏、导航栏等的位置尺寸的
而iOS7及之后的版本,屏幕view的位置尺寸默认是占据了整个屏幕
这里需要设置iOS6 和 iOS7, 控制view的frame坐标原点都是一样的
(1)iOS7 中 view的扩展效果
iOS7其实是增加了一个扩展属性,才能将屏幕view扩展到全屏幕
由于不是TableController或者ScrollController的view不需要滚动,所以不需要进行扩展
扩展属性:self.edgesForExtendedLayout
在storyboard修改扩展属性,取消扩展,默认使用iOS6的做法:
修改控制器的属性
取消勾选之后,会发现图片位置的Y是从导航栏下端开始的(跟iOS6一致)
运行效果:
#mark:由于系统是OXS10.10.1, Xcode6.1.1,这里我没有iOS6的模拟器,暂时试验不了iOS6的运行情况
(2)启动图的大小会决定app运行的大小
使用较小的启动图在较大尺寸的设备上运行,会出现黑边
例如在4英寸的设备上使用iOS6进行模拟,却最大只有3.5英寸 retina(2x)的图片,最后显示的效果就是3.5英寸设备的显示效果
给运行iOS6的4英寸设备增加retina4(4英寸retina)的启动图片
#mark:新出的iphone6(4.7英寸)和 iphone6 plus(5.5英寸)使用哪个尺寸的启动图片?
#A:经过试验证明是使用了 retina 4 的启动图,如果不存在就会使用 2x 的启动图,会造成上述的黑边效果
删掉 retina 4 的启动图
运行:
开启后:
7.设置按钮的背景
(1)背景图片拉伸方式(从中间某段开始拉伸)
之前在“聊天Demo”中,曾经使用过代码来设置UIImageView的图片拉伸方式(聊天框背景),其实UIImageView也可以在storyboard中进行拉伸设置:
Stretching(拉伸):
x: 左边需要保护的比例(右边由width影响)
y: 上边需要保护的比例(下边由height影响)
width:除去左边需要保护的部分,拉伸宽度部分的比例(0代表1像素)
height:除去上边需要保护的部分,拉伸高度部分的比例(0代表1像素)
在这里我们需要对一个UIButton进行拉伸,但是storyboard不能对UIButton进行此操作,会无效
----->所以需要使用代码来进行设置
stretchableImageWithLeftCapWidth
做成UIImage的一个分类方法
1//2//UIImage+Extend.m3//HelloLottery4//5//Created by hellovoidworld on 15/1/3.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"UIImage+Extend.h"1011@implementationUIImage(Extend)1213/** 返回一个中心扩展拉伸的图片*/14+ (UIImage *) resizableImage:(NSString *) imageName {15UIImage *image =[UIImage imageNamed:imageName];1617//这个参数决定了左边的保护区域,右边的保护区域为左边+1开始到末端18CGFloat width = image.size.width *0.5;1920//原理同左右保护区域21CGFloat height = image.size.height *0.5;2223//也就是,取中间1x1的区域作为扩展区域24return[image stretchableImageWithLeftCapWidth:width topCapHeight:height];25}2627@end
(2)在登陆控制器中设置按钮样式
1//2//HVWLoginViewController.m3//HelloLottery4//5//Created by hellovoidworld on 15/1/3.6//Copyright (c) 2015年 hellovoidworld. All rights reserved.7//89#import"HVWLoginViewController.h"10#import"UIImage+Extend.h"1112@interfaceHVWLoginViewController ()1314/** 登陆按钮*/15@property (weak, nonatomic) IBOutlet UIButton *loginButton;1617@end1819@implementationHVWLoginViewController2021- (void)viewDidLoad {22[super viewDidLoad];23//Do any additional setup after loading the view.2425UIImage *normal = [UIImage resizableImage:@"RedButton"];26UIImage *highlighted = [UIImage resizableImage:@"RedButtonPressed"];2728[self.loginButton setBackgroundImage:normal forState:UIControlStateNormal];29[self.loginButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];30}3132- (void)didReceiveMemoryWarning {33[super didReceiveMemoryWarning];34//Dispose of any resources that can be recreated.35}363738@end
8.storyboard添加“设置”界面(暂时演示用)
"我的彩票" --> 右上角 "设置"
使用TableViewController
使用static cell显示内容
使用“customer”为cell的样式,自行拖入image、label和switch等
9.设置按钮主题,统一设置Navigation导航栏按钮样式