iOS自定义键盘

对于有输入功能的控件,例如UITextField,可以给控件的inputView属性赋值,实现自定义键盘的功能

UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)];

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
[button setTitle:@"1" forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[view addSubview:button];

[button addTarget:self action:@selector(touchAction) forControlEvents:UIControlEventTouchUpInside];

_textField.inputView = view;

点击下载DEMO

也可以给控件的inputAccessoryView赋值UIToolbar的对象设置工具条。

如果结合第三方键盘监听事件的库IQKeyboard的话,效果更好。

你可能感兴趣的:(iOS自定义键盘)