您的位置:首页 > 移动开发 > IOS开发

iOS 网易彩票-3常见设置

2015-09-04 19:54 573 查看

Navigation导航设置

为了统一管理导航控制器,需要自定义导航控制器MJNavigationController,继承于UINavigationController。分别设置5个Navigation的控制器Class为此控制器。

白色状态栏

统一背景头部导航栏

设置所有Navigation导航栏字体颜色

二级页面隐藏底部导航条

1.白色状态栏。使用application管理状态栏

设置不使用控制器控制状态栏



在MJAppDelegate中设置:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
application.statusBarStyle=UIStatusBarStyleLightContent;

return YES;
}


这样会导致程序在启动的时候,有显示状态栏,如图



改进:

程序启动期间隐藏状态栏



程序启动完成显示状态栏

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
application.statusBarStyle=UIStatusBarStyleLightContent;
//显示导航栏
application.statusBarHidden=NO;
return YES;
}


2.统一背景头部导航栏,所有Navigation导航栏字体颜色及返回字体按钮颜色

MJNavigationController控制器的类initialize只会在类的第一次调用时使用,因此在这个方法里设置





/**
*  系统在第一次使用这个类的时候调用(1个类只会调用一次)
*/
+(void)initialize
{
//设置导航栏背景
UINavigationBar *navBar=[UINavigationBar appearance];
[navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];

//设置标题文字颜色
NSMutableDictionary *attrs=[NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
attrs[NSFontAttributeName]=[UIFont systemFontOfSize:16];
[navBar setTitleTextAttributes:attrs];

//设置BarButtonItem的主题
UIBarButtonItem *item=[UIBarButtonItem appearance];
NSMutableDictionary *itemAttrs=[NSMutableDictionary dictionary];
itemAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
itemAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:14];
[item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];

//设置BarButtonItem返回箭头颜色
navBar.tintColor=[UIColor whiteColor];
}


3.二级页面隐藏底部导航条

重写push方法,隐藏底部导航栏

/**
*  重写这个方法,拦截所有的push操作
*/
-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
viewController.hidesBottomBarWhenPushed=YES;
[super pushViewController:viewController animated:animated];
}


自定义导航栏标题按钮




不能选择UIBarButtonItem,UIBarButtonItem在storyboard中只能选择文字或者图片之一。


(1)选用UIButton,分别设置名称、图片、字体大小



(2)设置内间距及向右对齐



导航栏主题点击下拉菜单

效果:



1.使用UIButton作为title item



2.自定义该UIButton,交换按钮title和image的位置,实现titleRectForContentRect和imageRectForContentRect,控制内部控件的frame

//
//  MJTitleButton.m
//  Lottery
//
//  Created by jiangys on 15/9/4.
//  Copyright (c) 2015年 weconex. All rights reserved.
//

#import "MJTitleButton.h"

@interface MJTitleButton()
@property (nonatomic,strong) UIFont *titleFont;

@end

@implementation MJTitleButton

/**
*  从文件中解析一个对象的时候就会调用这个方法
*/
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self) {
[self setup];
}
return self;
}

/**
*  通过代码创建控件的时候就会调用
*/
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}

/**
*  初始化
*/
-(void)setup
{
self.titleFont=[UIFont systemFontOfSize:14];
self.titleLabel.font=self.titleFont;

//图片居中
self.imageView.contentMode=UIViewContentModeCenter;
}

/**
*  控制器内部label的frame
*  contentRect : 按钮自己的边框
*/
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloat titleX = 0;
CGFloat titleY = 0;
NSDictionary *attrs = @{NSFontAttributeName : self.titleFont};
CGFloat titleW;

titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;

CGFloat titleH = contentRect.size.height;
return CGRectMake(titleX, titleY, titleW, titleH);
}

/**
*  控制器内部imageView的frame
*/
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloat imageW = 30;
CGFloat imageX = contentRect.size.width - imageW;
CGFloat imageY = 0;
CGFloat imageH = contentRect.size.height;
return CGRectMake(imageX, imageY, imageW, imageH);
}
@end


3.自定义MJBuyViewController继承UIViewController的类,设置为该页面的控制器类,拖线监听title item点击事件







//
//  MJBuyViewController.m
//  Lottery
//
//  Created by jiangys on 15/9/4.
//  Copyright (c) 2015年 weconex. All rights reserved.
//

#import "MJBuyViewController.h"

@interface MJBuyViewController ()
/**
*  标题点击事件
*/
- (IBAction)titleClick:(UIButton *)sender;

@end

@implementation MJBuyViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (IBAction)titleClick:(UIButton *)sender {

//旋转箭头内容
[UIView animateWithDuration:0.25 animations:^{
sender.imageView.transform=CGAffineTransformRotate(sender.imageView.transform, M_PI);
}];

// 2.添加uiview
UIView *temp  = [[UIView alloc] init];
temp.frame = CGRectMake(100, 80, 100, 30);
temp.backgroundColor = [UIColor grayColor];
[self.view addSubview:temp];
}
@end


view的初始化方法

(1)awakeFromNib和initWithCoder:差别

awakeFromNib 从xib或者storyboard加载完毕就会调用

initWithCoder: 只要对象是从文件解析来的,就会调用

两个方法同时存在会先调用initWithCoder

(2)initWithCoder: & initWithFrame:

initWithCoder:使用文件加载的对象调用(如从xib或stroyboard中创建)

initWithFrame:使用代码加载的对象调用(使用纯代码创建)

注意:所以为了同时兼顾从文件和从代码解析的对象初始化,要同时在initWithCoder: 和 initWithFrame: 中进行初始化

设置图片背景

当前彩票只支持7.1以上的版本,设置很简单



当前,如果要适配iOS6的话,需要作简单的修改

(1)在storyboard修改扩展属性,取消扩展,默认使用iOS6的做法:



(2)取消勾选之后,会发现图片位置的Y是从导航栏下端开始的(跟iOS6一致)



设置按钮背景

(1)背景图片拉伸方式(从中间某段开始拉伸)
之前在“聊天Demo”中,曾经使用过代码来设置UIImageView的图片拉伸方式(聊天框背景),其实UIImageView也可以在storyboard中进行拉伸设置:



Stretching(拉伸):

x: 左边需要保护的比例(右边由width影响)

y: 上边需要保护的比例(下边由height影响)

width:除去左边需要保护的部分,拉伸宽度部分的比例(0代表1像素)

height:除去上边需要保护的部分,拉伸高度部分的比例(0代表1像素)

在这里我们需要对一个UIButton进行拉伸,但是storyboard不能对UIButton进行此操作,无效。因此,我们需要通过代码来实现
1.自定义一个控制器MJLoginViewController继承UIViewController



2.写一个图片扩展方法,方便以后在其它地方也可以使用。

UIImage+Extension.h

#import <UIKit/UIKit.h>

@interface UIImage (Extension)
+ (UIImage *)resizableImage:(NSString *)name;
@end


UIImage+Extension.m

#import "UIImage+Extension.h"

@implementation UIImage (Extension)
/**
*  返回一张可以随意拉伸不变形的图片
*
*  @param name 图片名字
*/
+ (UIImage *)resizableImage:(NSString *)name
{
UIImage *normal = [UIImage imageNamed:name];
CGFloat w = normal.size.width * 0.5;
CGFloat h = normal.size.height * 0.5;
return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
}
@end


3.在MJLoginViewController.m中设置按钮样式

//
//  MJLoginViewController.m
//  Lottery
//
//  Created by jiangys on 15/9/5.
//  Copyright (c) 2015年 weconex. All rights reserved.
//

#import "MJLoginViewController.h"
#import "UIImage+Extension.h"

@interface MJLoginViewController ()
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation MJLoginViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

UIImage *normal = [UIImage resizableImage:@"RedButton"];
UIImage *high = [UIImage resizableImage:@"RedButtonPressed"];

[self.loginBtn setBackgroundImage:normal forState:UIControlStateNormal];
[self.loginBtn setBackgroundImage:high forState:UIControlStateHighlighted];
}

@end


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: