首先看一下效果图
最近将旧代码好好整理一下,以适应新项目使用的ARC。
#pragma mark -
#pragma mark Text Field Delegate
// When you enter the user name,hide all of the date picker
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// InputAccessoryView
[textField setInputAccessoryView:[PublicFunction keyboardAccessory:@"keyboradAccessoryDoneButton" addTarget:self]];
}
// keyboard Accessory View Action
- (void)keyboradAccessoryDoneButton {
[self.emailText resignFirstResponder];
[self.passwordText resignFirstResponder];
}
上面代码部分
[PublicFunction keyboardAccessory:@"keyboradAccessoryDoneButton" addTarget:self]
就是我写的公用方法,先看完整的方法如下:
// keyboardAccessory
+ (UIView *)keyboardAccessory:(NSString *)doneButtonSelectorFromeString addTarget:(id)addTarget {
// 按扭
UIButton *keyboardAccessoryButtion = [[UIButton alloc] initWithFrame:CGRectMake(275, 0, 45, 29)];
[keyboardAccessoryButtion setImage:[UIImage imageNamed:@"keyboardAccessoryButtonImage"] forState:UIControlStateNormal];
[keyboardAccessoryButtion addTarget:addTarget action:NSSelectorFromString(doneButtonSelectorFromeString) forControlEvents:UIControlEventTouchUpInside];
// UIView
UIView *keyboardAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 29)];
[keyboardAccessoryView addSubview:keyboardAccessoryButtion];
return keyboardAccessoryView;
}
调用方法中的:中的 keyboradAccessoryDoneButton就是点击【箭头】时的触发的方法。代码相当简单,就是新建一个UIView,把按扭加到UIView绑定动作。
总结:这方法比在原有的键盘上硬加按扭要方便多了(我见过往数字键盘上加个DONE按扭的),基本上你只要 COPY,一贴就能用了。特别是在使用的 TextView 的时候,return 往往别当是成是换行,加个收缩按扭方便多了。