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;