這個是一個比較初級一點的文章,新人可以看看。當然實現這個需求的時候自己也有一點收獲,記下來吧。
前兩天產品要求在工程的所有數字鍵盤彈出時,上面帶一個小帽子,上面安裝一個“完成”按鈕,這個完成按鈕也沒有什么作用,點擊一下收回鍵盤就可以了。但是工程這么大,很多textfield彈出的都是數字鍵盤,不可能去每個VC里面修改每一個的代碼啊。
想到了一個比較好的辦法,自定義一個textfield,繼承系統的UITextField。這樣我自定義的textfield就有了系統UITextField的所有技能,再加上自己的所給他賦予的技能,這樣就夠了。
有了上面的想法就開始去嘗試,一開始首先想到的鍵盤彈出的時候會發出Notification,查了下基本有兩個通知可以接收到,UIKeyboardWillChangeFrameNotification和UIKeyboardDidChangeFrameNotification,接收到之后打印一下通知。內容很全,大約都有什么鍵盤彈出動畫事件,鍵盤的fream啊,芭啦芭啦很多了,自己可以去嘗試一下啊。然后想着做一個view,上面有個button,然后做個動畫,跟着鍵盤一塊上來下去,等等,這樣是不是有點麻煩了。
后來了解到了UITextField的inputAccessoryView這個屬性,這就比較方便了。設置一個view,就可以跟着鍵盤跑了,動畫不用做了。后來一看寫個view去適應屏幕等的方法也不是一兩行代碼能搞定的啊。於是又一不小心發現了UIToolbar,我之前真的沒有用過這個鬼東西。有了UIToolbar,都不用寫約束了,並且里面的button可以直接使使用UIBarButtonItem了,一般能使用UIBarButtonItem的地方也都不要約束的啊,比如NavigationBar上等。
但是,等等,僅僅實現一個小的收回鍵盤的功能也就太低端了吧。萬一以后產品說,點擊完成按鈕的同時要………………,好吧,那我就只能再進一步了,順帶給你提供上兩個回調吧,就delegate和block回調吧。實現回調的過程到不是很復雜,就是有一點讓我很不爽。竟然不能給系統的protocol UITextFieldDelegate添加協議方法,這直接讓我裝逼失敗啊。我只能使用delegateMy代替了,這樣打de的時候基本上就能出現提示了。據說在swift中可以給系統的protocol中添加方法了,應該會方便很多吧,最近剛開始研究swift。
不多說了,先放個圖看看


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.m
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