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 上附上了 ...
随机推荐
- Spark在Ubuntu中搭建开发环境
一.在Windows7中安装Ubuntu双系统 工具/原料 windows7 64位 ubuntu 16.04 32位 UltraISO最新版(用来将镜像文件烤到U盘中) 空U盘(若有文件,请先备 ...
- 每个程序员都会的35个jQuery小技巧!
1. 禁止右键点击$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...
- 微信JSSDK javascript 开发 代码片段,仅供参考
最全面最专业的微信公众平台开发教程:http://www.cnblogs.com/txw1958/p/weixin-js-sdk-demo.html 比较完整的分享教程:http://www.cnbl ...
- Ruby升级的最新方法
今天安装cocoapods时候出现了下面的提示Error installing pods:active support requires Ruby version >= 2.2.0这个需求的意思 ...
- Leetcode 255. Verify Preorder Sequence in Binary Search Tree
验证一个list是不是一个BST的preorder traversal sequence. Given an array of numbers, verify whether it is the co ...
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- elk系列7之通过grok分析apache日志
preface 说道分析日志,我们知道的采集方式有2种: 通过grok在logstash的filter里面过滤匹配. logstash --> redis --> python(py脚本过 ...
- JS-抽奖系统-实现原理
有本事中奖的,过来找我换红包!!哈哈!! <meta charset="UTF-8"> <title>抽奖系统</title> <styl ...
- CSS 概念 & 作用
http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html 式样定义 如何显示 HTML内容 通常存储在式样表中 作用 : 解 ...
- Jquery的 each的使用 $.each()
下面提一下each的几种常用的用法 1.each处理一维数组 var arr1 = [ "aaa", "bbb", "ccc" ]; $.e ...