iOS之ToolBar定制
ToorBar的定制
在诸如社区类的app里面,很多都涉及到用户发布消息,如现今最流行的新浪微博,每条信息底部都会有个工具条,正如下图所示,有转发、评论和点赞三个按钮的工具条。

结构
1.作为一个独立存在的toolBar,必须要有个View作为载体,这里选择用UIImageView;
2.toolBar有三个可以点击的按钮,所以就要创建三个button。
下面直接上代码了!
YJYStatusToolBar.h文件
#import <UIKit/UIKit.h>
@class YJYStatusToolBar;
@class YJYStatus;
typedef enum{
YJYStatusToolBarButtonTypeRetweet, //转发
YJYStatusToolBarButtonTypeComment, //评论
YJYStatusToolBarButtonTypeAttribude //点赞
}YJYStatusToolBarButtonType;
//设置代理方法,标记点击了哪个按钮
@protocol YJYStatusToolBarDelegate<NSObject>
-(void)statusToorBarButonClicked:(YJYStatusToolBarButtonType )type status:(YJYStatus *)status;
@end
@interface YJYStatusToolBar : UIImageView
@property (nonatomic, strong)YJYStatus *status; //传入的数据模型
@property (nonatomic, weak)id<YJYStatusToolBarDelegate>delegate;
@end
YJYStatusToolBar.m文件

在.m文件里的私有属性有两个可变数组,和三个按钮,数组通过懒加载创建
-(NSMutableArray *)btns{
if (!_btns) {
_btns = [NSMutableArray array];
}
return _btns;
}
-(NSMutableArray *)dividesV{
if (!_dividesV) {
_dividesV = [NSMutableArray array];
}
return _dividesV;
}
重写initWithFrame方法:UIImageView交互必须要打开(self.userInteractionEnabled = YES)
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self setupAllChildView];
self.userInteractionEnabled = YES;
self.image = [UIImage strechableImage:@"timeline_card_bottom_background"];
}
return self;
}
在init方法里调用setupAllChildView方法
-(void)setupAllChildView
{
UIButton *retweet = [self setupOneButtonWithImage:[UIImage imageNamed:@"timeline_icon_retweet"] title:@"转发" type:YJYStatusToolBarButtonTypeRetweet];
_retweet = retweet;
UIButton *comment = [self setupOneButtonWithImage:[UIImage imageNamed:@"timeline_icon_comment"] title:@"评论" type:YJYStatusToolBarButtonTypeComment];
_comment = comment;
UIButton *unlink = [self setupOneButtonWithImage:[UIImage imageNamed:@"timeline_icon_unlike"] title:@"赞" type:YJYStatusToolBarButtonTypeAttribude];
_unlink = unlink;
for (int i = 0; i < 2; i++) {
UIImageView *divideV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"timeline_card_bottom_line"]];
[self addSubview:divideV];
[self.dividesV addObject:divideV];
}
}
再抽调方法setupOneButtonWithImage: title: type:
-(UIButton *)setupOneButtonWithImage:(UIImage *)image title:(NSString *)title type:(YJYStatusToolBarButtonType)type
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:12];
button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
button.tag = type;
[button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
[self.btns addObject:button]; //每创建一个button就加入到数字里
return button;
}
由于在一个View的控件上添加了三个button而没有设置frame值,所以要layoutSubView一下
-(void)layoutSubviews{
[super layoutSubviews];
NSUInteger count = self.btns.count; //通过self.btn.count得出toolBar里button的数量,方便设置frame
CGFloat w = YJYScreenWidth / count;
CGFloat h = self.height;
CGFloat x = 0;
CGFloat y = 0;
for (int i = 0; i < count; i++) { //设置button的frame
UIButton *btn = self.btns[i];
x = w * i;
btn.frame = CGRectMake(x, y, w, h);
}
int i = 1;
for (UIImageView *divide in self.dividesV) { //设置间隔线的坐标
UIButton *btn = self.btns[i];
divide.x = btn.x;
i++;
}
}
实现代理方法:参数有type和status,即是按钮类型和这个消息的数据
-(void)btnClicked:(UIButton *)btn{
if ([_delegate respondsToSelector:@selector(statusToorBarButonClicked:status:)]) {
[_delegate statusToorBarButonClicked:(YJYStatusToolBarButtonType)btn.tag status:self.status];
}
}
因为toolBar上有转发数,评论数和点赞数,涉及到数据模型YJYStatusModel,所以实现一下setter的方法
-(void)setStatus:(YJYStatus *)status
{
_status = status;
[self setBtn:_retweet title:status.reposts_count];
[self setBtn:_comment title:status.comments_count];
[self setBtn:_unlink title:status.attitudes_count];
}
-(void)setBtn:(UIButton *)btn title:(int)count
{
NSString *title = nil;
if (count) {
if (count >= 10000) {
CGFloat floatCount = count / 10000.0;
title = [NSString stringWithFormat:@"%.1fW", floatCount];
title = [title stringByReplacingOccurrencesOfString:@".0" withString:@""];
}else{
title = [NSString stringWithFormat:@"%i", count];
}
[btn setTitle:title forState:UIControlStateNormal];
}
}
用到的工具类

UIImage+Image.m主要方法:图片渲染和图片拉伸的两个方法
+(instancetype)imageWithOriginName:(NSString *)imageName{
UIImage *image = [UIImage imageNamed:imageName];
return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}
+(instancetype)strechableImage:(NSString *)imageName{
UIImage *image = [UIImage imageNamed:imageName];
return [image stretchableImageWithLeftCapWidth:image.size.width / 2 topCapHeight:image.size.height / 2];
}
UIView+Frame.m 坐标的拓展方法:x.y.with.height.size五个坐标属性的getter和setter方法
- (CGFloat)width
{
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width
{
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)height
{
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height
{
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGFloat)x
{
return self.frame.origin.x;
}
- (void)setX:(CGFloat)x
{
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)y
{
return self.frame.origin.y;
}
- (void)setY:(CGFloat)y
{
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGSize)size
{
return self.frame.size;
}
- (void)setSize:(CGSize)size
{
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
iOS之ToolBar定制的更多相关文章
- iOS 怎么自定制推送声音呢?(APP运行时和APP进入后台时)
说明: 一般如果修改了apple官方的推送声音后,则APP进入后台后,推送会播放开发者自定制的推送声音,而用户在使用APP(也就是APP运行时)的时候,一般是不会有推送声音,因为此时的推送内容已经呈现 ...
- iOS写在定制相机之前
问题 不是所有的拍照UIImagePickerController都能搞定,理由如下: 1.产品不整点幺蛾子,哪来体验创新 2.设计不整点幺蛾子,怎能体现用心 3.运营:这体验跟某宝某信咋不一样??? ...
- iOS 键盘类型定制归纳
一.键盘风格 支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboardTypeASCIICapable, // ...
- iOS 改动toolbar里面文字的字体和大小
使用NSDictionaty来设置文本的属性: NSDictionary * attributes = @{NSFontAttributeName: [UIFont fontWithName:@&qu ...
- Xamarin iOS教程之添加和定制视图
Xamarin iOS教程之添加和定制视图 Xamarin iOS用户界面——视图 在iPhone或者iPad中,用户看到的摸到的都是视图.视图是用户界面的重要组成元素.例如,想要让用户实现文本输入时 ...
- iOS拍照定制之AVCaptureVideoDataOutput
问题 领导看了前面做的拍照,问了句"哪来的声音", "系统的,自带的,你看系统的拍照也有声音" "有办法能去掉吗?挺糟心的" "我 ...
- iOS拍照定制之AVCapturePhotoOutput
问题 领导安排任务,写个拍照功能,界面跟系统拍照有点出入 拍完照片,底部显示已拍照片,有个拍照上限[在此不论] 点击已拍照片,可以预览.放大缩小查看 思路 系统拍照肯定不行了,只能定制,没提是否拍照禁 ...
- iOS 视图控制器转场详解
iOS 视图控制器转场详解 前言的前言 唐巧前辈在微信公众号「iOSDevTips」以及其博客上推送了我的文章后,我的 Github 各项指标有了大幅度的增长,多谢唐巧前辈的推荐.有些人问我相关的问题 ...
- 笔记-iOS 视图控制器转场详解(上)
这是一篇长文,详细讲解了视图控制器转场的方方面面,配有详细的示意图和代码,为了使得文章在微信公众号中易于阅读,seedante 辛苦将大量长篇代码用截图的方式呈现,另外作者也在 Github 上附上了 ...
随机推荐
- Java多线程卖票例子
package com.test; public class SaleTickets implements Runnable { private int ticketCount = 10;// 总的票 ...
- Nginx基本使用
Nginx基本使用 下载源码包http://nginx.org/ http://nginx.org/en/download.html yum -y install pcre-devel openssl ...
- 快速上手php:使用PhpStrom部署项目
闲话 上学的时候一直以为可以专注自己的领域,以为与php无缘的,但是这种想法是错误,在完成任务的时候,你不得不使用你不熟悉的语言或者框架.正所谓业务驱动开发,这次接手已经离职的前辈的留下来的项目,最蛋 ...
- 在WildFly中运行多个standalone模式的实例
WildFly作为一款优秀的EJB容器,其前身为JBoss AS.JBoss作为一款开源的应用服务器,被广泛的应用在各种项目当中.假设我们现在有这样一个项目,他是以standalone的模式运行在 ...
- WEKA使用
参考 http://bbs.middleware123.com/thread-24052-1-1.html 使用Weka进行数据挖掘 http://quweiprotoss.blog.163.com ...
- ubuntu14.04 安装pip
参考链接: 1.http://www.liquidweb.com/kb/how-to-install-pip-on-ubuntu-14-04-lts/ 2.http://idroot.net/tuto ...
- VirtualBox Guest Additions 在CentOS中无法安装的解决方法
安装时出现一步错误查看log为(log文件是 /var/log/vboxadd-install.log): /tmp/vbox.0/Makefile.include.header:94: *** Er ...
- zabbix添加监控主机(三)
zabbix添加监控服务器. zabbix添加监控服务器(以添加10.10.100.137为例) (1)创建要监控的主机.点击配置(configuration)–>主机(host) –>创 ...
- 统计SqlServer每张表内的数据量
CREATE TABLE #temp (TableName VARCHAR (255), RowCnt INT)EXEC sp_MSforeachtable 'INSERT INTO #temp SE ...
- centos7 没有iptables服务 file or directory? 用secureCRT登录centos?
cenetos7 采用systemd来管理服务 centos7 没有采用传统的iptables服务, 而是采用的firewalld 服务, 以及firewall-cmd 命令; 也可以采用传统的 ip ...