效果图:
1.自定义一个TextField继承自UITextField:
.h文件
#import <UIKit/UIKit.h>
@interface AMPTextField : UITextField
@end
2.在.m文件中重写- (void)drawRect:(CGRect)rect {}方法
#import "AMPTextField.h"
@implementation AMPTextField
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, AMPScreenWidth,44)];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(AMPScreenWidth - 60, 7,50, 30)];
[button setTitle:@"完成"forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
button.layer.borderColor = [UIColor blueColor].CGColor;
button.layer.borderWidth =1;
button.layer.cornerRadius =3;
[bar addSubview:button];
self.inputAccessoryView = bar;
[button addTarget:self action:@selector(print)forControlEvents:UIControlEventTouchUpInside];
}
- (void) print {
[self resignFirstResponder];
}
@end
3.AMPTextField的键盘就会有完成按钮,点击完成收回键盘。