1、重写UITextField子类的drawRect方法
- (void)drawRect:(CGRect)rect {
// Drawing code
[super drawRect:rect];
UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 60, 7, 50, 30)];
[button setTitle:@"完成" forState:UIControlStateNormal];
[button setTitleColor:UIColorFromHex(0x1784d9) forState:UIControlStateNormal];
[button addTarget:self action:@selector(doneChoice) forControlEvents:UIControlEventTouchUpInside];
// ios11 后使用 UIBarButtonItem 管理,不然点击无效
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button]; UIBarButtonItem *flexibleitem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; NSArray *items = @[flexibleitem, flexibleitem, item];
[bar setItems:items animated:YES];
self.inputAccessoryView = bar;
}
2、storyBoard 中使用方法
1)控件正常拖线并建立属性
@property (weak, nonatomic) IBOutlet UITextField *tf_pw;
2)选择控件继承自你重写的子类
3、代码实现方法
@property (nonatomic, strong) JQTextField *tf_pw;