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的话,效果更好。



文/Fatm(简书作者)
原文链接:http://www.jianshu.com/p/dee8ef45a741
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。