IOS--- NavigationBar标题按钮
- 在“首页”的导航栏中部设置一个“首页”文字+箭头按钮
- 统一设置样式
- 根据实际文本长度调整宽度
- 消除系统自带的点击高亮效果
- 点击按钮,箭头上下颠倒

- 使用UIButton,设置文本和图片
- 在initWithFrame统一样式
- 创建一个继承UIButton的自定义类,重写文本和图片的绘图方法,互换位置
- 设置一个标识成员变量,判断当前的按钮状态(弹出 or 缩回)
1 //
2 // HVWNavigationBarTitleButton.m
3 // HVWWeibo
4 //
5 // Created by hellovoidworld on 15/2/2.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "HVWNavigationBarTitleButton.h"
10
11 @implementation HVWNavigationBarTitleButton
12
13 /** 重写initWithFrame, 统一设置按钮的样式 */
14 - (instancetype)initWithFrame:(CGRect)frame {
15 if (self = [super initWithFrame:frame]) {
16 // 设置字体
17 self.titleLabel.font = HVWNavigationTitleFont;
18 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
19
20 // 文本右对齐
21 [self.titleLabel setTextAlignment:NSTextAlignmentRight];
22
23 // 取消图标高亮效果
24 [self setAdjustsImageWhenDisabled:NO];
25
26 // 图片居中
27 [self.imageView setContentMode:UIViewContentModeCenter];
28
29 }
30
31 return self;
32 }
33
34 /** 重写iamge的绘图方法 */
35 - (CGRect)imageRectForContentRect:(CGRect)contentRect {
36 CGFloat height = contentRect.size.height;
37 CGFloat width = height;
38 CGFloat x = self.size.width - width;
39 CGFloat y = 0;
40 return CGRectMake(x, y, width, height);
41 }
42
43 /** 重写title的绘图方法 */
44 - (CGRect)titleRectForContentRect:(CGRect)contentRect {
45 CGFloat height = contentRect.size.height;
46 // 文本宽度 = 按钮整体宽度 - 图片宽度
47 CGFloat width = self.height - height;
48 CGFloat x = 0;
49 CGFloat y = 0;
50 return CGRectMake(x, y, width, height);
51 }
52
53 @end
1 - (void)viewDidLoad {
2 [super viewDidLoad];
3
4 // 添加导航控制器按钮
5 // 左边按钮
6 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_friendsearch" hightlightedImage:@"navigationbar_friendsearch_highlighted" target:self selector:@selector(searchFriend)];
7
8 // 右边按钮
9 self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"navigationbar_pop" hightlightedImage:@"navigationbar_pop_highlighted" target:self selector:@selector(pop)];
10
11 // 设置标题按钮
12 HVWNavigationBarTitleButton *titleButton = [[HVWNavigationBarTitleButton alloc] init];
13 titleButton.height = 35;
14 titleButton.width = 100;
15 [titleButton setTitle:@"首页" forState:UIControlStateNormal];
16 [titleButton setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
17 // 设置背景图片
18 [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
19
20 // 监听按钮点击事件,替换图标
21 [titleButton addTarget:self action:@selector(titleButtonClickd:) forControlEvents:UIControlEventTouchUpInside];
22
23 self.navigationItem.titleView = titleButton;
24 }
25
26 /** 标题栏按钮点击事件 */
27 - (void) titleButtonClickd:(UIButton *) button {
28 self.titleButtonExtended = !self.titleButtonExtended;
29
30 if (self.isTitleButtonExtended) {
31 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
32 } else {
33 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
34 }
35 }
36
37 #mark:有希望依赖图片缓存,使用"=="判断当前按钮图片来决定按钮状态的,发现使用currentImage和新建一个UIImage(同一张图片)出来的地址并不一致!所以不能采用。
38 -(void)titleButtonClick:(UIButton *)titleButton
39 {
40 UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"];
41
42 if (titleButton.currentImage==titleImage) {
43 //换成箭头向上
44 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
45 }else
46 {
47 //换成箭头向下
48 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
49 }
50 }

1 /** 标题栏按钮点击事件 */
2 - (void) titleButtonClickd:(UIButton *) button {
3 self.titleButtonExtended = !self.titleButtonExtended;
4
5 if (self.isTitleButtonExtended) {
6 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
7
8 // 弹出框
9 UIView *window = [[UIApplication sharedApplication] keyWindow];
10 UIImageView *popView = [[UIImageView alloc] init];
11 popView.size = CGSizeMake(200, 200);
12 popView.centerX = window.width * 0.5;
13 popView.y = 55;
14 popView.image = [UIImage resizedImage:@"popover_background"];
15 [self.navigationController.view addSubview:popView];
16
17 } else {
18 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
19 }
20 }

1 /** 标题栏按钮点击事件 */
2 - (void) titleButtonClickd:(UIButton *) button {
3 self.titleButtonExtended = !self.titleButtonExtended;
4
5 if (self.isTitleButtonExtended) {
6 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
7
8 UIView *window = [[UIApplication sharedApplication] keyWindow];
9
10 // 中间辅助覆盖层(帮助隐藏弹出框)
11 UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
12 coverLayer.frame = window.bounds;
13 coverLayer.backgroundColor = [UIColor blackColor];
14 coverLayer.alpha = 0.2;
15 [window addSubview:coverLayer];
16 [coverLayer addTarget:self action:@selector(coverLayerClicked:) forControlEvents:UIControlEventTouchUpInside];
17
18 // 弹出框
19 UIImageView *popView = [[UIImageView alloc] init];
20 self.popView = popView;
21 popView.size = CGSizeMake(200, 200);
22 popView.centerX = window.width * 0.5;
23 popView.y = 55;
24 popView.userInteractionEnabled = YES;
25
26 popView.image = [UIImage resizedImage:@"popover_background"];
27 [window addSubview:popView];
28
29 } else {
30 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
31 }
32 }
33
34 /** 辅助覆盖层点击事件 */
35 - (void) coverLayerClicked:(UIButton *) button {
36 if (self.isTitleButtonExtended) {
37 [button removeFromSuperview];
38 [self.popView removeFromSuperview];
39 [self titleButtonClickd:self.titleButton];
40 }
41 }

1 //
2 // HVWPopMenu.h
3 // HVWWeibo
4 //
5 // Created by hellovoidworld on 15/2/2.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import <UIKit/UIKit.h>
10
11 typedef enum {
12 PopMenuArrowLeft,
13 PopMenuArrowMid,
14 PopMenuArrowRight
15 } PopMenuArrow;
16
17 @class HVWPopMenu;
18 @protocol HVWPopMenuDelegate <NSObject>
19
20 @optional
21 - (void) popMenuDidHideMenu:(HVWPopMenu *) popMenu;
22
23 @end
24
25 @interface HVWPopMenu : UIView
26
27 /** 背景兼内容容器 */
28 @property(nonatomic, strong) UIImageView *backgroundContainer;
29
30 #pragma mark - 成员属性
31 /** 遮盖夹层 */
32 @property(nonatomic, strong) UIButton *coverLayer;
33
34 /** 内容控件 */
35 @property(nonatomic, strong) UIView *contentView;
36
37 /** 箭头位置 */
38 @property(nonatomic, assign) PopMenuArrow popMenuArrow;
39
40 /** 遮盖夹层透明标识 */
41 @property(nonatomic, assign, getter=isDimCoverLayer) BOOL dimCoverLayer;
42
43 /** 代理 */
44 @property(nonatomic, weak) id<HVWPopMenuDelegate> delegate;
45
46
47 #pragma mark - 初始化方法
48 - (instancetype) initWithContentView:(UIView *) contentView;
49 + (instancetype) popMenuWithContentView:(UIView *) contentView;
50
51 #pragma mark - 使用方法
52 /** 弹出 */
53 - (void) showMenuInRect:(CGRect) rect;
54
55 /** 隐藏 */
56 - (void) hideMenu;
57
58
59 @end
1 //
2 // HVWPopMenu.m
3 // HVWWeibo
4 //
5 // Created by hellovoidworld on 15/2/2.
6 // Copyright (c) 2015年 hellovoidworld. All rights reserved.
7 //
8
9 #import "HVWPopMenu.h"
10
11 @implementation HVWPopMenu
12
13 #pragma mark - 初始化方法
14 - (instancetype) initWithContentView:(UIView *) contentView {
15 if (self = [super init]) {
16 self.contentView = contentView;
17 }
18
19 return self;
20 }
21
22 + (instancetype) popMenuWithContentView:(UIView *) contentView {
23 return [[self alloc] initWithContentView:contentView];
24 }
25
26 /** 初始化子控件 */
27 - (instancetype)initWithFrame:(CGRect)frame {
28 self = [super initWithFrame:frame];
29 if (self) {
30 // 中间辅助覆盖层(帮助隐藏弹出框)
31 UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
32 self.coverLayer = coverLayer;
33 [self setDimCoverLayer:YES];
34 [coverLayer addTarget:self action:@selector(coverLayerClicked) forControlEvents:UIControlEventTouchUpInside];
35 [self addSubview:coverLayer];
36
37 // 添加背景容器
38 UIImageView *backgroundContainer = [[UIImageView alloc] init];
39 self.backgroundContainer = backgroundContainer;
40 backgroundContainer.userInteractionEnabled = YES;
41 [self setPopMenuArrow:PopMenuArrowMid];
42 [self addSubview:backgroundContainer];
43 }
44
45 return self;
46 }
47
48 /** 遮盖夹层点击事件 */
49 - (void) coverLayerClicked {
50 [self hideMenu];
51 }
52
53 #pragma mark - 使用方法
54 /** 弹出 */
55 - (void) showMenuInRect:(CGRect) rect {
56 // 准备添加到当前主窗口上
57 UIView *window = [[UIApplication sharedApplication] keyWindow];
58 self.frame = window.bounds;
59 [window addSubview:self];
60
61 self.coverLayer.frame = window.bounds;
62 self.backgroundContainer.frame = rect;
63
64 // 添加内容控件
65 if (self.contentView) {
66 CGFloat topMargin = 12;
67 CGFloat leftMargin = 5;
68 CGFloat bottomMargin = 8;
69 CGFloat rightMargin = 5;
70
71 self.contentView.x = leftMargin;
72 self.contentView.y = topMargin;
73 self.contentView.width = self.backgroundContainer.width - leftMargin - rightMargin;
74 self.contentView.height = self.backgroundContainer.height - topMargin - bottomMargin;
75
76 [self.backgroundContainer addSubview:self.contentView];
77 }
78 }
79
80 /** 隐藏 */
81 - (void) hideMenu {
82 if ([self.delegate respondsToSelector:@selector(popMenuDidHideMenu:)]) {
83 [self.delegate popMenuDidHideMenu:self];
84 }
85
86 [self removeFromSuperview];
87 }
88
89 #pragma mark - 特性设置
90 /** 设置遮盖夹层是否透明 */
91 - (void)setDimCoverLayer:(BOOL)dimCoverLayer {
92 if (dimCoverLayer) { // 需要半透明模糊效果的
93 self.coverLayer.backgroundColor = [UIColor blackColor];
94 self.coverLayer.alpha = 0.2;
95 } else { // 全透明
96 self.coverLayer.backgroundColor = [UIColor clearColor];
97 self.coverLayer.alpha = 1.0;
98 }
99 }
100
101
102 /** 设置弹出菜单顶部箭头位置:左、中、右 */
103 - (void)setPopMenuArrow:(PopMenuArrow)popMenuArrow {
104 _popMenuArrow = popMenuArrow;
105
106 switch (popMenuArrow) {
107 case PopMenuArrowLeft:
108 self.backgroundContainer.image = [UIImage resizedImage:@"popover_background_left"];
109 break;
110 case PopMenuArrowMid:
111 self.backgroundContainer.image = [UIImage resizedImage:@"popover_background"];
112 break;
113 case PopMenuArrowRight:
114 self.backgroundContainer.image = [UIImage resizedImage:@"popover_background_right"];
115 break;
116 default:
117 break;
118 }
119 }
120
121 @end
1 // HVWHomeViewController.m
2 /** 标题栏按钮点击事件 */
3 - (void) titleButtonClickd:(UIButton *) button {
4 self.titleButtonExtended = !self.titleButtonExtended;
5
6 if (self.isTitleButtonExtended) {
7 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
8
9 // 添加弹出菜单
10 UITableView *tableView = [[UITableView alloc] init];
11 HVWPopMenu *popMenu = [HVWPopMenu popMenuWithContentView:tableView];
12 popMenu.delegate = self;
13 popMenu.dimCoverLayer = YES; // 模糊遮盖
14 popMenu.popMenuArrow = PopMenuArrowMid; // 中部箭头
15
16 // 弹出
17 [popMenu showMenuInRect:CGRectMake(50, 55, 200, 300)];
18
19 } else {
20 [button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
21 }
22 }
23
24 #pragma mark - HVWPopMenuDelegate
25 - (void)popMenuDidHideMenu:(HVWPopMenu *)popMenu {
26 UIButton *titleButton = (UIButton *)self.navigationItem.titleView;
27 [self titleButtonClickd:titleButton];
28 }
IOS--- NavigationBar标题按钮的更多相关文章
- [iOS微博项目 - 1.5] - NavigationBar标题按钮
A.NavigationBar标题按钮 1.需求 在“首页”的导航栏中部设置一个“首页”文字+箭头按钮 统一设置样式 根据实际文本长度调整宽度 消除系统自带的点击高亮效果 点击按钮,箭头上下颠倒 gi ...
- iOS 添加导航按钮
iOS设置导航按钮navigationBar中包含了这几个重要组成部分:leftBarButtonItem, rightBarButtonItem, backBarButtonItem, title. ...
- UINavigationController-自定义导航栏标题按钮.
见视频0416 自定义导航栏标题按钮,在Bar Button Item中加入UIButton,设置UIButton的图片和标题,还可以自定义自定义UIButton实现特效按钮.
- Apple iOS 触控按钮 自动关闭 bug
Apple iOS 触控按钮 自动关闭 bug bug 轻点 iPhone 背面可执行操作 您可以轻点两下或轻点三下 iPhone 背面以执行某些操作,如向上或向下滚动.截屏.打开"控制中心 ...
- IOS让返回按钮显示自定义标题而不是上个ViewController的title
在开发IOS的时候发现当从一个ViewController跳转到另一个ViewController时,被跳转到的那个ViewController的返回按钮总是显示的是上个ViewController的 ...
- iOS 自定义NavigationBar右侧按钮rightBarButtonItem--button
//两个按钮的父类view UIView *rightButtonView = [[UIView alloc] initWithFrame:CGRectMake(, , , )]; //历史浏览按钮 ...
- iOS 自定义NavigationBar右侧按钮rightBarButtonItem
自定义右侧的一个按钮 UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"主页" style: ...
- 【转】自定义iOS的Back按钮(backBarButtonItem)和pop交互手势(interactivepopgesturerecognizer) --- 不错
原文网址:http://blog.csdn.net/joonsheng/article/details/41362499 序 说到自定义UINavigetionController的返回按钮,iOS7 ...
- iOS导航标题不居中问题(转载)
前言 一直以来都让我很头痛的一个问题:系统自带的导航条,在标题文字很长时,进入到下一个界面,而下一个界面的标题也很长时,就会出现标题不居中显示. 曾经,我尝试过很多种办法,但是都没有从根上解决问题.下 ...
随机推荐
- 使用 DiskMaker X 轻松制作 Yosemite 安装 U 盘(引)
使用 DiskMaker X 轻松制作 Yosemite 安装 U 盘 由于帮人在MacBook上装Windows, 用pe格式化了一下分出来的Windows分区, 搞得Mac系统也瘫掉了, 无奈之下 ...
- java数组或集合返回空的问题
在有返回值的情况下,平时我写代码基本遇到错误什么都是返回null,我因为我觉得把数组或集合这个初始化占空间. 但是我发现这样在每次客户段调用都要进行非空判断,而且有时调用内置api还容易报错误,于是解 ...
- 【HDOJ】2062 Subset sequence
这道题目非常好,饶了点儿圈子.我的思路是,先按照组排列.例如,1 2 31 2 2 1 3 11 2 3 2 1 3 ...
- 搭建Git本地服务器
搭建Git本地服务器 参考文章:http://www.ossxp.com/doc/git/gitolite.html 当前任务,学习中... 公司小范围用法: 服务器上做的: .在服务器上建立一个用户 ...
- bzoj3262
三维裸的做法是一维排序,剩下树套树,可我好像还没写过树套树先说cdq分治吧,先对一维排序,相当于原来修改询问里的时间线在这上面分治.划分,计算前半部分对后半部分的影响,显然可以按第二维的顺序维护树状数 ...
- BZOJ_1617_[Usaco2008_Mar]_River_Crossing_渡河问题_(动态规划)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1617 FJ要把\(n\)头奶牛用船运到河对岸.FJ自己在船上要用\(m\)时间,如果运送\(i ...
- 做一个自己的最小Linux系统
一.前言 Linux操作系统至1991.10.5号诞生以来,就源其开源性和自由性得到了很多技术大牛的青睐,每个Linux爱好者都为其贡献了自己的一份力,不管是在Linux内核还是开源软件等方面,都为 ...
- rfc all download
http://www.rfc-editor.org/download.html about RTSP http://en.wikipedia.org/wiki/Real_Time_Streaming_ ...
- [转]ASP.NET MVC 入门5、View与ViewData
view在MVC模式中与用户进行最直接的接触,它负责数据的呈现.这里要注意一点就是,view只是负责数据的呈现,所以我们应该要尽量让view中不涉及业务逻辑的处理. 我们来添加一个Blog首页的vie ...
- 经典sql总结(1)
1.表示info 信息,字段为Id和res,如何得到如下结果