## 一:效果

## 二:封裝好的工具條
### 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)發送微博調用相機里面的圖片以及調用相機