ios中自定義鍵盤+回收
//? 自定義鍵盤
UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 256)];
field.inputView = keyView;
// 1、添加一個(gè)按鈕
UIButton *keyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
keyBtn.frame = CGRectMake(0, 0, 50, 40);
[keyBtn addTarget:self action:@selector(haha:)? ? ? ? ? ? forControlEvents:UIControlEventTouchUpInside];
[keyBtn setTitle:@"1" forState:UIControlStateNormal];
keyBtn.backgroundColor = [UIColor whiteColor];
[keyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[keyView addSubview:keyBtn];
// 1.1點(diǎn)擊按鈕,實(shí)現(xiàn)可以讓按鈕的標(biāo)題顯示在輸入框中
- (void)haha: (UIButton *)sender{
//? 得到輸入框
UITextField? *fiel = (UITextField *)[self.window viewWithTag:2000];
// 得到按鈕標(biāo)題,因?yàn)闃?biāo)題是個(gè)字符串,所以得到的結(jié)果得讓字符串承接
NSString *titel = [sender titleForState:UIControlStateNormal];
//? ? 將標(biāo)題顯示在輸入框中
//? ? fiel.text = titel;
//? ? 拼接字符,也可以使用這種拼接的方式,來連續(xù)輸入
fiel.text = [fiel.text stringByAppendingString:titel];
}
//2、 添加一個(gè)回收鍵盤的按鈕
初始化創(chuàng)建? ? UIButton *keyBtn1 = [UIButton buttonWithType:UIButtonTypeSystem];
設(shè)置大小 keyBtn1.frame = CGRectMake(100, 0, 50, 40);
添加關(guān)聯(lián)方法? [keyBtn1 addTarget:self action:@selector(huishou:) forControlEvents:UIControlEventTouchUpInside];
設(shè)置標(biāo)題 [keyBtn1 setTitle:@"Return" forState:UIControlStateNormal];
設(shè)置背景色? keyBtn1.backgroundColor = [UIColor whiteColor];
設(shè)置標(biāo)題顏色 [keyBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
呈現(xiàn)? [keyView addSubview:keyBtn1];
//回收鍵盤的按鈕實(shí)現(xiàn)方法
- (void)huishou:(UIButton *)sender{
得到textfield? UITextField? *fiel = (UITextField *)[self.window viewWithTag:2000];
//? 回收鍵盤,取消輸入框的第一響應(yīng)者,結(jié)束輸入框的編輯狀態(tài)
[fiel resignFirstResponder];
}