在您的应用程序中使用自定义键盘时,只需使用 UITextField 的 inputView 属性。
您可以通过以下方式执行此操作:
@interface SCAViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property ( nonatomic) UIView *labelView;
@end
@implementation SCAViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// here we set the keyboard to become the first responder
// this is optional...
//[_textField becomeFirstResponder];
// creating the custom label keyboard
_labelView = [[UIView alloc] initWithFrame:CGRectMake(0, 568, 320, 568/3)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
label.text = @"Label View!";
[_labelView addSubview:label];
_labelView.backgroundColor = [UIColor blueColor];
_textField.inputView = _labelView;
}
现在解释一下:
_labelView 设置为当前尺寸的原因是因为我想要与默认键盘紧密匹配。您可以随时将它们更改为您需要的任何内容。
我在视图中添加了一个标签,但是由于您正在使用表情符号,因此我将创建一个方法,该方法将使用按钮将图像加载到 textField 中。
列表如下:
- 为按钮创建操作方法
- 创建按钮
- 设置按钮的背景图片
- 在自定义视图上添加按钮
- 将 inputView 设置为自定义视图
你可以像这样创建动作方法
// create the method for the button
- (IBAction) loadOntoKeyboard {
// load the image/text/whatever else into the textfield
}
您可以创建一个按钮以添加到您的自定义视图中,例如
// creating custom button
UIButton *emoji1 = [UIButton buttonWithType:UIButtonTypeCustom];
// creating frame for button.
// x - the location of the button in x coordinate plane
// y - same as x, but on y plane
// width - width of button
// height - height of button
emoji1.frame = CGRectMake(x, y, width, height);
// just setting background image here
[emoji1 setBackgroundImage:[UIImage imageNamed:@"emoji_image…"] forState:UIControlStateNormal];
// don't forget to add target of button
[emoji1 addTarget:self action:@selector(loadOntoKeyboard) forControlEvents:UIControlEventTouchUpInside];
// adding button onto view
[customView addSubview:emoji1];
// setting the inputView to the custom view where you added the buttons
textFieldVariable.inputView = customView;
希望这一切对你有意义,我希望我能帮助你清楚地了解如何实现自定义键盘操作 :)
希望这会有所帮助!