ios中自定义键盘+回收
// 自定义键盘
UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, 256)];
field.inputView = keyView;
// 1、添加一个按钮
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点击按钮,实现可以让按钮的标题显示在输入框中
- (void)haha: (UIButton *)sender{
// 得到输入框
UITextField *fiel = (UITextField *)[self.window viewWithTag:2000];
// 得到按钮标题,因为标题是个字符串,所以得到的结果得让字符串承接
NSString *titel = [sender titleForState:UIControlStateNormal];
// 将标题显示在输入框中
// fiel.text = titel;
// 拼接字符,也可以使用这种拼接的方式,来连续输入
fiel.text = [fiel.text stringByAppendingString:titel];
}
//2、 添加一个回收键盘的按钮
初始化创建 UIButton *keyBtn1 = [UIButton buttonWithType:UIButtonTypeSystem];
设置大小 keyBtn1.frame = CGRectMake(100, 0, 50, 40);
添加关联方法 [keyBtn1 addTarget:self action:@selector(huishou:) forControlEvents:UIControlEventTouchUpInside];
设置标题 [keyBtn1 setTitle:@"Return" forState:UIControlStateNormal];
设置背景色 keyBtn1.backgroundColor = [UIColor whiteColor];
设置标题颜色 [keyBtn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
呈现 [keyView addSubview:keyBtn1];
//回收键盘的按钮实现方法
- (void)huishou:(UIButton *)sender{
得到textfield UITextField *fiel = (UITextField *)[self.window viewWithTag:2000];
// 回收键盘,取消输入框的第一响应者,结束输入框的编辑状态
[fiel resignFirstResponder];
}
网友评论