iOS11 下鍵盤右上角完成按鈕點擊無效處理

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;

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。