对于有输入功能的控件,例如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;
也可以给控件的inputAccessoryView赋值UIToolbar的对象设置工具条。
如果结合第三方键盘监听事件的库IQKeyboard的话,效果更好。
本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1766790,如需转载请自行联系原作者