[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 ...
随机推荐
- 【转载】Java中如何写一段内存泄露的程序 & ThreadLocal 介绍和使用
可以参考这段文章: link A1:通过以下步骤可以很容易产生内存泄露(程序代码不能访问到某些对象,但是它们仍然保存在内存中): 上文中提到了使用ThreadLocal造成了内存泄露,但是写的不清不楚 ...
- poj 1845 Sumdiv (数论)
题目链接 题意:求 A^B的所有约数之和对9901取模后的结果. 分析: 看了小优的博客写的. 分析来自 http://blog.csdn.net/lyy289065406/article/detai ...
- 类Item_field
class Item_field :public Item_ident { protected: void set_field(Field *field); public: Field *field, ...
- Selenium Tutorial (2) - Selenium IDE In Depth
Installing Firefox and Firebug Installing and Opening Selenium IDE Starting with test cases and test ...
- UVALive 5713 Qin Shi Huang's National Road System(次小生成树)
题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...
- 怎么制作生成苹果手机app应用的下载二维码图片
原文网址:http://jingyan.baidu.com/article/8065f87ff654262331249886.html app store应用生成二维码操作步骤: 1.首先在MAC上的 ...
- Android好用且常用的插件及工具
1.GitHub,这个不管是做安卓还是其他,只要是开发就必上的网站,也是天朝没有墙掉为数不多的网站 2.Stack OverFlow,这个和上面一样,国外非常著名的问答网站,在上面基本上很多问题都可以 ...
- 六款最佳Linux教育应用
导读 对教育行业的用户来说,有好几款专门的Linux发行版是专门面向教育行业的.本文将介绍适合教育领域的几款顶级发行版. 1.Edubuntu 位居榜首的是Edubuntu.顾名思义,Edubuntu ...
- Maximum Product Subarray JAVA实现
题目描述: Find the contiguous subarray within an array (containing at least one number) which has the la ...
- 数据仓库之ETL漫谈
ETL,Extraction-Transformation-Loading的缩写,中文名称为数据抽取.转换和加载. 大多数据仓库的数据架构可以概括为: 数据源-->ODS(操作型数据存储)--& ...