iOS学习之----------自定义工具条
2015-05-29 22:49
411 查看
在某些时候我们输入文字后可能需要加入一些图标,图片等其他形式内容,因此要在UITextView或UITextField上添加工具条,进行操作
设计原则:将需要添加的工具都设置为button并放在一个UIView上,将UITextView或UITextField 的inputAccessoryView 属性设置成该View即可显示。
1、定义toolBar类对其进行封装,外部只能对其创建 不能修改 ,而且 当该类里面的内容发生改变的时候,外部不会受影响
设置背景颜色 通过一张很小的图片,在整个View上平铺这张图片
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
2、添加子控件
[self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:yzComposeToolBarButtonTypeCamera];
//调用这个方法创建button并设置button在不同点击状态下的图片
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(yzComposeToolBarButtonType)tag
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
[button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
[self addSubview:button];
[button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = tag;
}
3、点击button的响应事件
1》当点击button后控制器获取该点击事件,因此需要用代理,控制器成为toolbar的代理获得点击事件。
2》控制器还需要知道点击了toolBar中的哪个button 因此需要对点击的button进行判断 ,控制器可以通过重写代理方法并根据参数判断是哪个button,这里使用枚举的方式,在创建button的时候 给每个button设置一个枚举值 ,控制器重写方法后,可以根据传出的buttonType判断点击了哪个按钮,而且在ToolBar类中 发生的改变 控制器不用去关心
枚举
typedef enum {
yzComposeToolBarButtonTypeCamera,
yzComposeToolBarButtonTypeEmoticon,//表情
yzComposeToolBarButtonTypeMention,
yzComposeToolBarButtonTypePicture,
yzComposeToolBarButtonTypeTrend,//话题
}yzComposeToolBarButtonType;
@class yzComposeToolBar;
代理
@protocol yzComposeToolBarDelegate <NSObject>
@optional
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType;
@end
@interface yzComposeToolBar : UIView
@property (nonatomic,weak)id<yzComposeToolBarDelegate>delegate;
@end
button的点击事件
- (void)buttonOnClick:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(composeToolBar:DidClickedButton:)]) {
[self.delegate composeToolBar:self DidClickedButton:button.tag];
}
}
4、控制器中创建toolbar 并设置代理
- (void)setupToolBar
{
//创建
yzComposeToolBar *toolBar = [[yzComposeToolBar alloc]init];
toolBar.width = self.view.width;
toolBar.height = 44;
toolBar.delegate = self;
self.textView.inputAccessoryView = toolBar;
}
控制器对button点击事件的响应和判断
#pragma mark -- 代理方法
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType
{
switch (buttonType) {
case yzComposeToolBarButtonTypeCamera:
NSLog(@"照相机");
break;
case yzComposeToolBarButtonTypeEmoticon:
NSLog(@"表情");
break;
case yzComposeToolBarButtonTypeMention:
NSLog(@"");
break;
case yzComposeToolBarButtonTypePicture:
NSLog(@"");
break;
case yzComposeToolBarButtonTypeTrend:
NSLog(@"");
break;
}
}
设计原则:将需要添加的工具都设置为button并放在一个UIView上,将UITextView或UITextField 的inputAccessoryView 属性设置成该View即可显示。
1、定义toolBar类对其进行封装,外部只能对其创建 不能修改 ,而且 当该类里面的内容发生改变的时候,外部不会受影响
设置背景颜色 通过一张很小的图片,在整个View上平铺这张图片
self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithName:@"compose_toolbar_background"]];
2、添加子控件
[self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:yzComposeToolBarButtonTypeCamera];
//调用这个方法创建button并设置button在不同点击状态下的图片
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(yzComposeToolBarButtonType)tag
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageWithName:icon] forState:UIControlStateNormal];
[button setImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted];
[self addSubview:button];
[button addTarget:self action:@selector(buttonOnClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = tag;
}
3、点击button的响应事件
1》当点击button后控制器获取该点击事件,因此需要用代理,控制器成为toolbar的代理获得点击事件。
2》控制器还需要知道点击了toolBar中的哪个button 因此需要对点击的button进行判断 ,控制器可以通过重写代理方法并根据参数判断是哪个button,这里使用枚举的方式,在创建button的时候 给每个button设置一个枚举值 ,控制器重写方法后,可以根据传出的buttonType判断点击了哪个按钮,而且在ToolBar类中 发生的改变 控制器不用去关心
枚举
typedef enum {
yzComposeToolBarButtonTypeCamera,
yzComposeToolBarButtonTypeEmoticon,//表情
yzComposeToolBarButtonTypeMention,
yzComposeToolBarButtonTypePicture,
yzComposeToolBarButtonTypeTrend,//话题
}yzComposeToolBarButtonType;
@class yzComposeToolBar;
代理
@protocol yzComposeToolBarDelegate <NSObject>
@optional
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType;
@end
@interface yzComposeToolBar : UIView
@property (nonatomic,weak)id<yzComposeToolBarDelegate>delegate;
@end
button的点击事件
- (void)buttonOnClick:(UIButton *)button
{
if ([self.delegate respondsToSelector:@selector(composeToolBar:DidClickedButton:)]) {
[self.delegate composeToolBar:self DidClickedButton:button.tag];
}
}
4、控制器中创建toolbar 并设置代理
- (void)setupToolBar
{
//创建
yzComposeToolBar *toolBar = [[yzComposeToolBar alloc]init];
toolBar.width = self.view.width;
toolBar.height = 44;
toolBar.delegate = self;
self.textView.inputAccessoryView = toolBar;
}
控制器对button点击事件的响应和判断
#pragma mark -- 代理方法
- (void)composeToolBar:(yzComposeToolBar *)toolBar DidClickedButton:(yzComposeToolBarButtonType)buttonType
{
switch (buttonType) {
case yzComposeToolBarButtonTypeCamera:
NSLog(@"照相机");
break;
case yzComposeToolBarButtonTypeEmoticon:
NSLog(@"表情");
break;
case yzComposeToolBarButtonTypeMention:
NSLog(@"");
break;
case yzComposeToolBarButtonTypePicture:
NSLog(@"");
break;
case yzComposeToolBarButtonTypeTrend:
NSLog(@"");
break;
}
}
相关文章推荐
- iPhone:iOS界面,本地生成随机验证码
- AsyncSocket的用法
- 新手入门iOS,浅谈学习经验
- IOS 点击空白处隐藏键盘的几种方法
- ios数据保存
- 苹果开发 笔记(32)
- iOS战记 ----OC基础语法之战(一)
- iOS开发学习第七课——结构体的定义、声明和应用
- iOS:各release版本的特性(持续更新)
- Windows通过VNC或SSH远程登录Mac
- Windows 远程控制 Mac 的解决方案
- C++随笔 —— std::setprecision与showpoint和ios::fixed的配合使用
- IOS-OC中获取某个数的绝对值
- 获取静态TableView选中的cell来执行相应操作
- IOS CopyPNGFile 异常问题解决
- 关于Xcode6模拟器 键盘不显示和没有中文输入法的解决方案
- iOS常用设计模式——工厂方法(简单工厂模式,工厂方法模式, 抽象工厂模式)
- IOS注册消息后应多加注意
- IOS开发二维码生成及扫描ZBarDemo
- 5月整理IOS