有时候遇到数字键盘需要添加完成收回键盘的时间,下面我整理的一个UIToolBar的分类。
+ (UIToolbar *)addToolbarWithtarget:(id)target Action: (SEL)action{
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 35)];
toolBar.tintColor = kColorRgb(237, 191, 72);
toolBar.backgroundColor = [UIColor whiteColor];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:target action:action];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:target action:action];
cancelBtn.tintColor = [UIColor grayColor];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolBar.items = @[cancelBtn,space,doneBtn];
return toolBar;
}
对分类(category)的理解:
分类的主要作用.在不修改原来类的基础上,增加方法,主要是系统自带的类,对封装代码代码有很大的帮助,也很方便。其次要注意分类只能扩展方法,不能增加属性和成员变量。如果添加了,奔溃的原因是没有自动为我们设置的属性生成set、get方法、、
上面的分类方法就是增加在UIToolBar的基础上。
分类(category)和类扩展(extension)的区别
参考这篇文章,会有比较好的理解http://www.cocoachina.com/ios/20161018/17784.html