这个是一个比较初级一点的文章,新人能够看看。固然实现这个需求的时候本身也有一点收获,记下来吧。git
前两天产品要求在工程的全部数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有什么做用,点击一下收回键盘就能够了。可是工程这么大,不少textfield弹出的都是数字键盘,不可能去每一个VC里面修改每个的代码啊。swift
想到了一个比较好的办法,自定义一个textfield,继承系统的UITextField。这样我自定义的textfield就有了系统UITextField的全部技能,再加上本身的所给他赋予的技能,这样就够了。ide
有了上面的想法就开始去尝试,一开始首先想到的键盘弹出的时候会发出Notification,查了下基本有两个通知能够接收到,UIKeyboardWillChangeFrameNotification和UIKeyboardDidChangeFrameNotification,接收到以后打印一下通知。内容很全,大约都有什么键盘弹出动画事件,键盘的fream啊,芭啦芭啦不少了,本身能够去尝试一下啊。而后想着作一个view,上面有个button,而后作个动画,跟着键盘一块上来下去,等等,这样是否是有点麻烦了。动画
后来了解到了UITextField的inputAccessoryView这个属性,这就比较方便了。设置一个view,就能够跟着键盘跑了,动画不用作了。后来一看写个view去适应屏幕等的方法也不是一两行代码能搞定的啊。因而又一不当心发现了UIToolbar,我以前真的没有用过这个鬼东西。有了UIToolbar,都不用写约束了,而且里面的button能够直接使使用UIBarButtonItem了,通常能使用UIBarButtonItem的地方也都不要约束的啊,好比NavigationBar上等。atom
可是,等等,仅仅实现一个小的收回键盘的功能也就过低端了吧。万一之后产品说,点击完成按钮的同时要………………,好吧,那我就只能再进一步了,顺带给你提供上两个回调吧,就delegate和block回调吧。实现回调的过程到不是很复杂,就是有一点让我很不爽。居然不能给系统的protocol UITextFieldDelegate添加协议方法,这直接让我装逼失败啊。我只能使用delegateMy代替了,这样打de的时候基本上就能出现提示了。听说在swift中能够给系统的protocol中添加方法了,应该会方便不少吧,最近刚开始研究swift。spa
很少说了,先放个图看看.net
1 #import <UIKit/UIKit.h> 2 @class MyTextField; 3 typedef void(^TapDoneButton)(MyTextField * mytextfield); 4 @protocol MyTextFieldDelegate; 5 @interface MyTextField : UITextField 6 @property(nonatomic,weak)id<MyTextFieldDelegate>delegateMy; 7 -(void)tapDoneButtonBlock:(TapDoneButton)aBlock; 8 @end 9 @protocol MyTextFieldDelegate <NSObject> 10 @optional 11 -(void)didTapDoneButton:(MyTextField *)textfield; 12 @end
MyTextField.mcode
1 #import "MyTextField.h" 2 @interface MyTextField () 3 @property(nonatomic,strong)TapDoneButton tapdonebutton; 4 @end 5 @implementation MyTextField 6 /* 7 // Only override drawRect: if you perform custom drawing. 8 // An empty implementation adversely affects performance during animation. 9 - (void)drawRect:(CGRect)rect { 10 // Drawing code 11 } 12 */ 13 - (instancetype)initWithCoder:(NSCoder *)coder 14 { 15 self = [super initWithCoder:coder]; 16 if (self) { 17 self.keyboardType = UIKeyboardTypeDecimalPad; 18 UIToolbar * toobar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 38.0f)]; 19 toobar.translucent = YES; 20 toobar.barStyle = UIBarStyleDefault; 21 UIBarButtonItem * spaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 22 UIBarButtonItem * doneBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard:)]; 23 [toobar setItems:@[spaceBarButtonItem,doneBarButtonItem]]; 24 self.inputAccessoryView = toobar; 25 } 26 return self; 27 } 28 - (void)resignKeyboard:(id)sender 29 { 30 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil]; 31 if (self.tapdonebutton) { 32 self.tapdonebutton(self); 33 } 34 if ([self.delegateMy respondsToSelector:@selector(didTapDoneButton:)]) { 35 [self.delegateMy didTapDoneButton:self]; 36 } 37 } 38 -(void)tapDoneButtonBlock:(TapDoneButton)aBlock 39 { 40 self.tapdonebutton = aBlock; 41 } 42 @end