## 一:效果
![](https://box.kancloud.cn/2016-01-20_569f1d9a17e42.jpg)
## 二:封装好的工具条
### NYComposeToolbar.h
带代理方法
~~~
#import <UIKit/UIKit.h>
typedef enum {
NYComposeToolbarButtonTypeCamera, // 拍照
NYComposeToolbarButtonTypePicture, // 相册
NYComposeToolbarButtonTypeMention, // @
NYComposeToolbarButtonTypeTrend, // #
NYComposeToolbarButtonTypeEmotion // 表情
} NYComposeToolbarButtonType;
@class NYComposeToolbar;
@protocol NYComposeToolbarDelegate <NSObject>
@optional
- (void)composeToolbar:(NYComposeToolbar *)toolbar didClickButton:(NYComposeToolbarButtonType)buttonType;
@end
@interface NYComposeToolbar : UIView
@property (nonatomic, weak) id<NYComposeToolbarDelegate> delegate;
@end
~~~
### NYComposeToolbar.m
~~~
//
// NYComposeToolbar.m
// 黑马微博2期
//
// Created by apple on 14-10-20.
// Copyright (c) 2014年 heima. All rights reserved.
//
#import "NYComposeToolbar.h"
@implementation NYComposeToolbar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_toolbar_background"]];
// 初始化按钮
[self setupBtn:@"compose_camerabutton_background" highImage:@"compose_camerabutton_background_highlighted" type:NYComposeToolbarButtonTypeCamera];
[self setupBtn:@"compose_toolbar_picture" highImage:@"compose_toolbar_picture_highlighted" type:NYComposeToolbarButtonTypePicture];
[self setupBtn:@"compose_mentionbutton_background" highImage:@"compose_mentionbutton_background_highlighted" type:NYComposeToolbarButtonTypeMention];
[self setupBtn:@"compose_trendbutton_background" highImage:@"compose_trendbutton_background_highlighted" type:NYComposeToolbarButtonTypeTrend];
[self setupBtn:@"compose_emoticonbutton_background" highImage:@"compose_emoticonbutton_background_highlighted" type:NYComposeToolbarButtonTypeEmotion];
}
return self;
}
/**
* 创建一个按钮
*/
- (void)setupBtn:(NSString *)image highImage:(NSString *)highImage type:(NYComposeToolbarButtonType)type
{
UIButton *btn = [[UIButton alloc] init];
[btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = type;
[self addSubview:btn];
}
- (void)layoutSubviews
{
[super layoutSubviews];
// 设置所有按钮的frame
NSUInteger count = self.subviews.count;
CGFloat btnW = self.width / count;
CGFloat btnH = self.height;
for (NSUInteger i = 0; i<count; i++) {
UIButton *btn = self.subviews[i];
btn.y = 0;
btn.width = btnW;
btn.x = i * btnW;
btn.height = btnH;
}
}
- (void)btnClick:(UIButton *)btn
{
if ([self.delegate respondsToSelector:@selector(composeToolbar:didClickButton:)]) {
// NSUInteger index = (NSUInteger)(btn.x / btn.width);
[self.delegate composeToolbar:self didClickButton:btn.tag];
}
}
@end
~~~
## 三:调用
设置代理并且实现代理方法
~~~
/**
* 添加工具条
*/
- (void)setupToolbar
{
NYComposeToolbar *toolbar = [[NYComposeToolbar alloc] init];
toolbar.width = self.view.width;
toolbar.height = 44;
toolbar.y = self.view.height - toolbar.height;
toolbar.delegate = self;
[self.view addSubview:toolbar];
self.toolbar = toolbar;
}
~~~
代理方法
~~~
#pragma mark - NYComposeToolbarDelegate
- (void)composeToolbar:(NYComposeToolbar *)toolbar didClickButton:(NYComposeToolbarButtonType)buttonType
{
switch (buttonType) {
case NYComposeToolbarButtonTypeCamera: // 拍照
// [self openCamera];
NYLog(@"--- 拍照");
break;
case NYComposeToolbarButtonTypePicture: // 相册
NYLog(@"--- 相册");
// [self openAlbum];
break;
case NYComposeToolbarButtonTypeMention: // @
NYLog(@"--- @");
break;
case NYComposeToolbarButtonTypeTrend: // #
NYLog(@"--- #");
break;
case NYComposeToolbarButtonTypeEmotion: // 表情\键盘
NYLog(@"--- 表情");
break;
}
}
~~~
- 前言
- (1)微博主框架-子控制器的添加
- (2)微博主框架-自定义导航控制器NavigationController
- (3)微博主框架-UIImage防止iOS7之后自动渲染_定义分类
- (4)微博自定义tabBar中间的添加按钮
- (5)微博自定义搜索框searchBar
- (6)导航控制器NavigationController 的滑动回退功能实现
- (7)程序启动新特性用UICollectionViewController实现
- (8)用AFNetworking和SDWebImage简单加载微博数据
- (9)微博模型之时间相关重要操作,判断刚刚,昨天,今年等等
- (10)微博cell中图片的显示以及各种填充模式简介
- (11)发送微博自定义TextView实现带占位文字
- (12)发送微博自定义工具条代理实现点击事件
- (13)发送微博调用相机里面的图片以及调用相机