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

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

/** 标题栏按钮点击事件 */
- (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonExtended; if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; // 弹出框
UIView *window = [[UIApplication sharedApplication] keyWindow];
UIImageView *popView = [[UIImageView alloc] init];
popView.size = CGSizeMake(, );
popView.centerX = window.width * 0.5;
popView.y = ;
popView.image = [UIImage resizedImage:@"popover_background"];
[self.navigationController.view addSubview:popView]; } else {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
}
}

/** 标题栏按钮点击事件 */
- (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonExtended; if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; UIView *window = [[UIApplication sharedApplication] keyWindow]; // 中间辅助覆盖层(帮助隐藏弹出框)
UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
coverLayer.frame = window.bounds;
coverLayer.backgroundColor = [UIColor blackColor];
coverLayer.alpha = 0.2;
[window addSubview:coverLayer];
[coverLayer addTarget:self action:@selector(coverLayerClicked:) forControlEvents:UIControlEventTouchUpInside]; // 弹出框
UIImageView *popView = [[UIImageView alloc] init];
self.popView = popView;
popView.size = CGSizeMake(, );
popView.centerX = window.width * 0.5;
popView.y = ;
popView.userInteractionEnabled = YES; popView.image = [UIImage resizedImage:@"popover_background"];
[window addSubview:popView]; } else {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
}
} /** 辅助覆盖层点击事件 */
- (void) coverLayerClicked:(UIButton *) button {
if (self.isTitleButtonExtended) {
[button removeFromSuperview];
[self.popView removeFromSuperview];
[self titleButtonClickd:self.titleButton];
}
}

//
// HVWPopMenu.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/2.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> typedef enum {
PopMenuArrowLeft,
PopMenuArrowMid,
PopMenuArrowRight
} PopMenuArrow; @class HVWPopMenu;
@protocol HVWPopMenuDelegate <NSObject> @optional
- (void) popMenuDidHideMenu:(HVWPopMenu *) popMenu; @end @interface HVWPopMenu : UIView /** 背景兼内容容器 */
@property(nonatomic, strong) UIImageView *backgroundContainer; #pragma mark - 成员属性
/** 遮盖夹层 */
@property(nonatomic, strong) UIButton *coverLayer; /** 内容控件 */
@property(nonatomic, strong) UIView *contentView; /** 箭头位置 */
@property(nonatomic, assign) PopMenuArrow popMenuArrow; /** 遮盖夹层透明标识 */
@property(nonatomic, assign, getter=isDimCoverLayer) BOOL dimCoverLayer; /** 代理 */
@property(nonatomic, weak) id<HVWPopMenuDelegate> delegate; #pragma mark - 初始化方法
- (instancetype) initWithContentView:(UIView *) contentView;
+ (instancetype) popMenuWithContentView:(UIView *) contentView; #pragma mark - 使用方法
/** 弹出 */
- (void) showMenuInRect:(CGRect) rect; /** 隐藏 */
- (void) hideMenu; @end
//
// HVWPopMenu.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/2.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWPopMenu.h" @implementation HVWPopMenu #pragma mark - 初始化方法
- (instancetype) initWithContentView:(UIView *) contentView {
if (self = [super init]) {
self.contentView = contentView;
} return self;
} + (instancetype) popMenuWithContentView:(UIView *) contentView {
return [[self alloc] initWithContentView:contentView];
} /** 初始化子控件 */
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// 中间辅助覆盖层(帮助隐藏弹出框)
UIButton *coverLayer = [UIButton buttonWithType:UIButtonTypeCustom];
self.coverLayer = coverLayer;
[self setDimCoverLayer:YES];
[coverLayer addTarget:self action:@selector(coverLayerClicked) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:coverLayer]; // 添加背景容器
UIImageView *backgroundContainer = [[UIImageView alloc] init];
self.backgroundContainer = backgroundContainer;
backgroundContainer.userInteractionEnabled = YES;
[self setPopMenuArrow:PopMenuArrowMid];
[self addSubview:backgroundContainer];
} return self;
} /** 遮盖夹层点击事件 */
- (void) coverLayerClicked {
[self hideMenu];
} #pragma mark - 使用方法
/** 弹出 */
- (void) showMenuInRect:(CGRect) rect {
// 准备添加到当前主窗口上
UIView *window = [[UIApplication sharedApplication] keyWindow];
self.frame = window.bounds;
[window addSubview:self]; self.coverLayer.frame = window.bounds;
self.backgroundContainer.frame = rect; // 添加内容控件
if (self.contentView) {
CGFloat topMargin = ;
CGFloat leftMargin = ;
CGFloat bottomMargin = ;
CGFloat rightMargin = ; self.contentView.x = leftMargin;
self.contentView.y = topMargin;
self.contentView.width = self.backgroundContainer.width - leftMargin - rightMargin;
self.contentView.height = self.backgroundContainer.height - topMargin - bottomMargin; [self.backgroundContainer addSubview:self.contentView];
}
} /** 隐藏 */
- (void) hideMenu {
if ([self.delegate respondsToSelector:@selector(popMenuDidHideMenu:)]) {
[self.delegate popMenuDidHideMenu:self];
} [self removeFromSuperview];
} #pragma mark - 特性设置
/** 设置遮盖夹层是否透明 */
- (void)setDimCoverLayer:(BOOL)dimCoverLayer {
if (dimCoverLayer) { // 需要半透明模糊效果的
self.coverLayer.backgroundColor = [UIColor blackColor];
self.coverLayer.alpha = 0.2;
} else { // 全透明
self.coverLayer.backgroundColor = [UIColor clearColor];
self.coverLayer.alpha = 1.0;
}
} /** 设置弹出菜单顶部箭头位置:左、中、右 */
- (void)setPopMenuArrow:(PopMenuArrow)popMenuArrow {
_popMenuArrow = popMenuArrow; switch (popMenuArrow) {
case PopMenuArrowLeft:
self.backgroundContainer.image = [UIImage resizedImage:@"popover_background_left"];
break;
case PopMenuArrowMid:
self.backgroundContainer.image = [UIImage resizedImage:@"popover_background"];
break;
case PopMenuArrowRight:
self.backgroundContainer.image = [UIImage resizedImage:@"popover_background_right"];
break;
default:
break;
}
} @end
// HVWHomeViewController.m
/** 标题栏按钮点击事件 */
- (void) titleButtonClickd:(UIButton *) button {
self.titleButtonExtended = !self.titleButtonExtended; if (self.isTitleButtonExtended) {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_up"] forState:UIControlStateNormal]; // 添加弹出菜单
UITableView *tableView = [[UITableView alloc] init];
HVWPopMenu *popMenu = [HVWPopMenu popMenuWithContentView:tableView];
popMenu.delegate = self;
popMenu.dimCoverLayer = YES; // 模糊遮盖
popMenu.popMenuArrow = PopMenuArrowMid; // 中部箭头 // 弹出
[popMenu showMenuInRect:CGRectMake(, , , )]; } else {
[button setImage:[UIImage imageWithNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
}
} #pragma mark - HVWPopMenuDelegate
- (void)popMenuDidHideMenu:(HVWPopMenu *)popMenu {
UIButton *titleButton = (UIButton *)self.navigationItem.titleView;
[self titleButtonClickd:titleButton];
}
[iOS微博项目 - 1.5] - NavigationBar标题按钮的更多相关文章
- [iOS微博项目 - 3.4] - 获取用户信息
github: https://github.com/hellovoidworld/HVWWeibo A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上 ...
- [iOS微博项目 - 1.4] - 各种item NavigationBar & NavigationItem & BarButtonItem || TabBar & TabBarItem
一.UINavigationItem1> 获得方式self.navigationItem // self是指控制器2> 作用可以用来设置当前控制器顶部导航栏的内容// 设置导航栏中间的内容 ...
- [iOS微博项目 - 3.1] - 发微博界面
github: https://github.com/hellovoidworld/HVWWeibo A.发微博界面:自定义UITextView 1.需求 用UITextView做一个编写微博的输 ...
- [iOS微博项目 - 1.0] - 搭建基本框架
A.搭建基本环境 github: https://github.com/hellovoidworld/HVWWeibo 项目结构: 1.使用代码构建UI,不使用storyboard ...
- [iOS微博项目 - 4.5] - 每条微博的底部工具条
github: https://github.com/hellovoidworld/HVWWeibo A.每条微博的底部工具条 1.需求 每条微博底部都有一个工具条 显示3个按钮:评论.转发.赞 按钮 ...
- [iOS微博项目 - 3.6] - 获取未读消息
github: https://github.com/hellovoidworld/HVWWeibo A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...
- [iOS微博项目 - 3.0] - 手动刷新微博
github: https://github.com/hellovoidworld/HVWWeibo A.下拉刷新微博 1.需求 在“首页”界面,下拉到一定距离的时候刷新微博数据 刷新数据的时候使 ...
- [iOS微博项目 - 1.7] - 版本新特性
A.版本新特性 1.需求 第一次使用新版本的时候,不直接进入app,而是展示新特性界面 github: https://github.com/hellovoidworld/HVWWeibo ...
- [iOS微博项目 - 1.6] - 自定义TabBar
A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...
随机推荐
- Provider Pattern提供者模式和策略模式
http://www.codeproject.com/Articles/18222/Provider-Pattern Introduction Provider pattern is one of t ...
- java实现最基础的socket网络通信
一.网络通信基础 网络中存在很多的通信实体,每一个通信实体都有一个标识符就是IP地址. 而现实中每一个网络实体可以和多个通信程序同时进行网络通信,这就需要使用端口号进行区分. 二.java中的基本网络 ...
- IIS与ASP.NET中的队列
一.IIS:应用程序池队列(Application pool queue,位于HTTP.SYS) 这是请求到达IIS后遇到的第一个队列,http.sys收到请求后会将请求放入对应的应用程序池队列,这样 ...
- Java知识点:javac命令
javac命令初窥 注:以下红色标记的参数在下文中有所讲解. 用法: javac <options> <source files> 其中, 可能的选项包括: -g ...
- UVALive 4287 Proving Equivalences(缩点)
等价性问题,给出的样例为 a->b的形式,问要实现全部等价(即任意两个可以互相推出),至少要加多少个形如 a->b的条件. 容易想到用强连通缩点,把已经实现等价的子图缩掉,最后剩余DAG. ...
- poj 1087 A Plug for UNIX
题目描述:现在由你负责布置Internet联合组织首席执行官就职新闻发布会的会议室.由于会议室修建时被设计成容纳全世界各地的新闻记者,因此会议室提供了多种电源插座用以满足(会议室修建时期)各国不同插头 ...
- css清除浮动的两种方式(clearfix和clear)
最近总是在用浮动,这两种方式总是浮现在眼前,或者说去掉父级和同级浮动样式总在思考中.两种方式怎么写都在base.css中. 在做瑞祥之旅的过程中,还是吃了一个大亏,就是清除浮动,不管是同级还是父级,都 ...
- 开源Jabber(XMPP) IM服务器介绍
一.摘要 这是我粗略读了一遍Jabber协议和相关技术文章后的产物,有些地方不一定准确.在文章中引用的一些代码来自www.jabber.org上的文章. 二. 什么是Jabber Jabber就 ...
- HDU 5874 Friends and Enemies
Friends and Enemies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- EasyHook远注简单监控示例 z
http://www.csdn 123.com/html/itweb/20130827/83559_83558_83544.htm 免费开源库EasyHook(inline hook),下面是下载地址 ...