Navigation导航设置

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

  • 白色状态栏
  • 统一背景头部导航栏
  • 设置所有Navigation导航栏字体颜色
  • 二级页面隐藏底部导航条

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

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

在MJAppDelegate中设置:

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. // Override point for customization after application launch.
  4. application.statusBarStyle=UIStatusBarStyleLightContent;
  5.  
  6. return YES;
  7. }

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

改进:

程序启动期间隐藏状态栏

程序启动完成显示状态栏

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

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

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

  1. /**
  2. * 系统在第一次使用这个类的时候调用(1个类只会调用一次)
  3. */
  4. +(void)initialize
  5. {
  6. //设置导航栏背景
  7. UINavigationBar *navBar=[UINavigationBar appearance];
  8. [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
  9.  
  10. //设置标题文字颜色
  11. NSMutableDictionary *attrs=[NSMutableDictionary dictionary];
  12. attrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
  13. attrs[NSFontAttributeName]=[UIFont systemFontOfSize:];
  14. [navBar setTitleTextAttributes:attrs];
  15.  
  16. //设置BarButtonItem的主题
  17. UIBarButtonItem *item=[UIBarButtonItem appearance];
  18. NSMutableDictionary *itemAttrs=[NSMutableDictionary dictionary];
  19. itemAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
  20. itemAttrs[NSFontAttributeName]=[UIFont systemFontOfSize:];
  21. [item setTitleTextAttributes:itemAttrs forState:UIControlStateNormal];
  22.  
  23. //设置BarButtonItem返回箭头颜色
  24. navBar.tintColor=[UIColor whiteColor];
  25. }

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

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

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

自定义导航栏标题按钮

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

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

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

导航栏主题点击下拉菜单

效果:

1.使用UIButton作为title item

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

  1. //
  2. // MJTitleButton.m
  3. // Lottery
  4. //
  5. // Created by jiangys on 15/9/4.
  6. // Copyright (c) 2015年 weconex. All rights reserved.
  7. //
  8.  
  9. #import "MJTitleButton.h"
  10.  
  11. @interface MJTitleButton()
  12. @property (nonatomic,strong) UIFont *titleFont;
  13.  
  14. @end
  15.  
  16. @implementation MJTitleButton
  17.  
  18. /**
  19. * 从文件中解析一个对象的时候就会调用这个方法
  20. */
  21. - (instancetype)initWithCoder:(NSCoder *)decoder
  22. {
  23. self = [super initWithCoder:decoder];
  24. if (self) {
  25. [self setup];
  26. }
  27. return self;
  28. }
  29.  
  30. /**
  31. * 通过代码创建控件的时候就会调用
  32. */
  33. - (instancetype)initWithFrame:(CGRect)frame
  34. {
  35. self = [super initWithFrame:frame];
  36. if (self) {
  37. [self setup];
  38. }
  39. return self;
  40. }
  41.  
  42. /**
  43. * 初始化
  44. */
  45. -(void)setup
  46. {
  47. self.titleFont=[UIFont systemFontOfSize:];
  48. self.titleLabel.font=self.titleFont;
  49.  
  50. //图片居中
  51. self.imageView.contentMode=UIViewContentModeCenter;
  52. }
  53.  
  54. /**
  55. * 控制器内部label的frame
  56. * contentRect : 按钮自己的边框
  57. */
  58. -(CGRect)titleRectForContentRect:(CGRect)contentRect
  59. {
  60. CGFloat titleX = ;
  61. CGFloat titleY = ;
  62. NSDictionary *attrs = @{NSFontAttributeName : self.titleFont};
  63. CGFloat titleW;
  64.  
  65. titleW = [self.currentTitle boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
  66.  
  67. CGFloat titleH = contentRect.size.height;
  68. return CGRectMake(titleX, titleY, titleW, titleH);
  69. }
  70.  
  71. /**
  72. * 控制器内部imageView的frame
  73. */
  74. -(CGRect)imageRectForContentRect:(CGRect)contentRect
  75. {
  76. CGFloat imageW = ;
  77. CGFloat imageX = contentRect.size.width - imageW;
  78. CGFloat imageY = ;
  79. CGFloat imageH = contentRect.size.height;
  80. return CGRectMake(imageX, imageY, imageW, imageH);
  81. }
  82. @end

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

  1. //
  2. // MJBuyViewController.m
  3. // Lottery
  4. //
  5. // Created by jiangys on 15/9/4.
  6. // Copyright (c) 2015年 weconex. All rights reserved.
  7. //
  8.  
  9. #import "MJBuyViewController.h"
  10.  
  11. @interface MJBuyViewController ()
  12. /**
  13. * 标题点击事件
  14. */
  15. - (IBAction)titleClick:(UIButton *)sender;
  16.  
  17. @end
  18.  
  19. @implementation MJBuyViewController
  20.  
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. }
  25.  
  26. - (IBAction)titleClick:(UIButton *)sender {
  27.  
  28. //旋转箭头内容
  29. [UIView animateWithDuration:0.25 animations:^{
  30. sender.imageView.transform=CGAffineTransformRotate(sender.imageView.transform, M_PI);
  31. }];
  32.  
  33. // 2.添加uiview
  34. UIView *temp = [[UIView alloc] init];
  35. temp.frame = CGRectMake(, , , );
  36. temp.backgroundColor = [UIColor grayColor];
  37. [self.view addSubview:temp];
  38. }
  39. @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

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface UIImage (Extension)
  4. + (UIImage *)resizableImage:(NSString *)name;
  5. @end

UIImage+Extension.m

  1. #import "UIImage+Extension.h"
  2.  
  3. @implementation UIImage (Extension)
  4. /**
  5. * 返回一张可以随意拉伸不变形的图片
  6. *
  7. * @param name 图片名字
  8. */
  9. + (UIImage *)resizableImage:(NSString *)name
  10. {
  11. UIImage *normal = [UIImage imageNamed:name];
  12. CGFloat w = normal.size.width * 0.5;
  13. CGFloat h = normal.size.height * 0.5;
  14. return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(h, w, h, w)];
  15. }
  16. @end

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

  1. //
  2. // MJLoginViewController.m
  3. // Lottery
  4. //
  5. // Created by jiangys on 15/9/5.
  6. // Copyright (c) 2015年 weconex. All rights reserved.
  7. //
  8.  
  9. #import "MJLoginViewController.h"
  10. #import "UIImage+Extension.h"
  11.  
  12. @interface MJLoginViewController ()
  13. @property (weak, nonatomic) IBOutlet UIButton *loginBtn;
  14.  
  15. @end
  16.  
  17. @implementation MJLoginViewController
  18.  
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view.
  22.  
  23. UIImage *normal = [UIImage resizableImage:@"RedButton"];
  24. UIImage *high = [UIImage resizableImage:@"RedButtonPressed"];
  25.  
  26. [self.loginBtn setBackgroundImage:normal forState:UIControlStateNormal];
  27. [self.loginBtn setBackgroundImage:high forState:UIControlStateHighlighted];
  28. }
  29.  
  30. @end

iOS 网易彩票-3常见设置的更多相关文章

  1. iOS 网易彩票-4设置模块一

    概述 基本上,每一款APP都有相应的设置模块.怎么设置才能更灵活和通用呢,这也是大家一直思考的.下面说说在网易彩票中,设置模块的设置思想. 基本上有三种方案: static cell(呆板,完全没有动 ...

  2. iOS 网易彩票-6设置模块三(常用小功能)

    该篇文章中,用到很多iOS开发过程中常用的小功能,当前只是将这些功能集成到网易彩票的设置中.iOS-常用小功能介绍,请参考我的另一篇文章: iOS 常用小功能 总结:http://www.cnblog ...

  3. iOS 网易彩票-1框架搭建

    仿网易彩票,最终要做成的效果如下: 一.分层搭建 1.新建一个项目,Lottery.只支持7.1以上坚屏. 2.将素材全部图片全部拉到相应的文件夹里. 3.选中Lottery--右键Show in F ...

  4. iOS 网易彩票-5设置模块二

    产品推荐 产品推荐使用的是UICollectionView控件,UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视 ...

  5. iOS 网易彩票-2框架搭建-代码重构

    在上一篇中,我们基本已经把整个框架都搭建出来了,下面进行代码重构一下. 思路: 导航按钮,按下时,会变灰,那是系统自带了,通过自定义UIButton,实现按下按钮立即切换效果. MJTabBarCon ...

  6. iOS菜鸟成长笔记(2)——网易彩票练习

    距离上一篇<第一个iOS应用>已经有一个多月了,今天来和大家一起学习和分享一下一个小练习<网易彩票> 首先我们向storyboard中拖入一个TabBarController和 ...

  7. iOS开发——实战总结OC篇&网易彩票开发知识点总结

    网易彩票开发知识点总结 关于网易彩票开发中遇到了不少的坑,弄了好久才弄懂,或者有些犹豫很久没用就不记得了,所以这里就总结了一下,希望以后不会忘记,就算忘记也能快速查看! /************** ...

  8. 再造轮子之网易彩票-第一季(IOS 篇 by sixleaves)

    前言 在网上看了别人做的模仿网易彩票的项目, 于是也跟着用自己的想法做了一篇.写这篇博客的目的, 在于UI综合的一次小练习, 同时总结和串联其各个控件之间的应用.封装思想等.考虑到有人上不了githu ...

  9. iOS 开发笔记-UILable/UIFont/UIButton常见设置

    UILabel的常见设置 @property(nonatomic,copy) NSString *text; 显示的文字 @property(nonatomic,retain) UIFont *fon ...

随机推荐

  1. VIM 如何使用系统的剪切板

    想要将系统剪贴板里的内容复制到 vi 编辑的文档中怎么办? 例如,在网页上复制了一段文字,想贴到本地的某个文件中. 使用 vi 打开本地文件,在 输入 模式下,按 Shift + Insert 详细可 ...

  2. 关于 CommonJS AMD CMD UMD 规范的差异总结(转)

    根据CommonJS规范,一个单独的文件就是一个模块.每一个模块都是一个单独的作用域,也就是说,在一个文件定义的变量(还包括函数和类),都是私有的,对其他文件是不可见的. // foo.js var ...

  3. 为android编译libsocket的脚本

    #!/bin/bash U32=0 #编译64位arm时 U32=0   编译32位arm时 U32=1 其他参数不需要变动 TARGET=android-24 HOST=darwin-x86_64 ...

  4. java(7)LinkedList源码

    系统环境 JDK1.7 LinkedList的基本结构 :在JDK1.6中LinkedList是双向引用的环形结构,JDK1.6中是双向引用的线性结构 提醒:看链表代码时最好用笔画下链表结构 有助于理 ...

  5. Oracle中V$SESSION等各表的字段解释,Oracle官方解释

    一.常用的视图 1.会话相关视图 View Description V$PROCESS Contains information about the currently active processe ...

  6. tcp连接出现close_wait状态?可能是代码不够健壮

    一.问题概述 今天遇到个小问题. 我们的程序依赖了大数据那边的服务,大数据那边提供了restful接口供我们调用. 测试反映接口有问题,我在本地重现了. 我这边感觉抓包可能对分析问题有用,就用wire ...

  7. 初学lua --lua嵌入c++的一个问题(初始化lua出错,版本问题)

    初学lua.从http://lua-users.org/wiki/CallingLuaFromCpp上下载了一个lua嵌入C++的代码.编译并运行.发现有错误: PANIC: unprotected ...

  8. Capistrano 部署rails 应用

    1 安装 gem install capistrano // For mutiple stages gem install capistrano-ext 2 准备 capify . 这个命令会创建Ca ...

  9. Xcode - LLDB调试技巧

    LLDB是Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍. ...

  10. Bagging和Boosting的概念与区别

    随机森林属于集成学习(ensemble learning)中的bagging算法,在集成算法中主要分为bagging算法与boosting算法, Bagging算法(套袋发) bagging的算法过程 ...