iOS学习之----------自定义工具条
来源:互联网 发布:矢量软件是什么 编辑:程序博客网 时间:2024/04/30 04:26
在某些时候我们输入文字后可能需要加入一些图标,图片等其他形式内容,因此要在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;
}
}
- iOS学习之----------自定义工具条
- extjs 之columntree 自定义分页工具条
- extjs 之columntree 自定义分页工具条
- (0065)iOS开发之工具条UIToolBar
- iOS学习之----------自定义UITextView
- IOS学习之自定义UITableViewCell
- 猫猫学iOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
- AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件
- FckEditor自定义工具条
- 自定义工具条一
- AE自定义工具条代码
- iOS_22自定义键盘工具条
- openlayers3自定义工具条
- 自定义键盘工具条
- UEditor自定义toolbar工具条
- IOS开发UI篇之──键盘添加工具条
- IOS开发UI篇之──键盘添加工具条
- IOS开发UI篇之──键盘添加工具条
- CodeForces 520E. Pluses everywhere 字符串,组合数学
- 笔记
- 黄陂胆囊结石科哪个看的好
- [ACdream]哗啦啦族的24点游戏[dfs][暴力][精度]
- 鄂州胆囊结石如何治疗比较好
- iOS学习之----------自定义工具条
- 随州治疗胆囊结石哪家比较专业
- 孝感看胆囊结石哪里好
- Round-robin
- c++14现行标准
- loki库之内存池SmallObj
- 潜江哪家胆囊结石最好
- Java Object类讲解 上帝类及其一些方法的分析
- 页面元素中内容显示在最上面