自定义 KeyBoard 示例

Objective-C 和 Xib

将目标添加到现有 XCode 项目

StackOverflow 文档

在添加目标中,选择 Custom KeyBoard StackOverflow 文档

像这样添加目标:

StackOverflow 文档

你的项目文件目录应该如下所示

StackOverflow 文档

这里 myKeyBoard 是添加的 Target 的名称

添加类型为 UIView 的新 Cocoatouch 文件并添加接口文件

StackOverflow 文档

最后,你的项目目录应如下所示

StackOverflow 文档

使 keyBoardView.xib 成为 keyBoardView 的子类

StackOverflow 文档

keyBoardView.xib 文件中创建界面

StackOverflow 文档

建立从 keyBoardView.xibkeyBoardView.h 文件的连接

keyBoardView.h 应该是这样的

#import <UIKit/UIKit.h>

@interface keyBoardView : UIView

@property (weak, nonatomic) IBOutlet UIButton *deleteKey;
//IBOutlet for the delete Key
@property (weak, nonatomic) IBOutlet UIButton *globe;
//Outlet for the key with title globe which changes the keyboard type
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *keys;
//Contains a colloection of all the keys '0 to 9' '+' '-' and '.'

@end

keyBoardViewController.h 文件中导入 #import "keyBoardView.h"

声明键盘 @property (strong, nonatomic)keyBoardView *keyboard; 的属性

评论出来

@property (nonatomic, strong) UIButton *nextKeyboardButton and all the code associated with it

KeyboardViewController.m 文件的 viewDidLoad() 函数应该如下所示

- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyboard=[[[NSBundle mainBundle]loadNibNamed:@"keyBoardView" owner:nil options:nil]objectAtIndex:0];
      self.inputView=self.keyboard;
    [self addGestureToKeyboard];

    // Perform custom UI setup here
//    self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];
//    
//    [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
//    [self.nextKeyboardButton sizeToFit];
//    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;
//    
//    [self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:self.nextKeyboardButton];
//    
//    [self.nextKeyboardButton.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
//    [self.nextKeyboardButton.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor].active = YES;
}

函数 addGestureToKeyboardpressDeleteKeykeyPressed 定义如下

-(void) addGestureToKeyboard
{
    [self.keyboard.deleteKey addTarget:self action:@selector(pressDeleteKey) forControlEvents:UIControlEventTouchUpInside];
    [self.keyboard.globe addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
    
    for (UIButton *key in self.keyboard.keys)
    {
        [key addTarget:self action:@selector(keyPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}
-(void) pressDeleteKey
{
    [self.textDocumentProxy deleteBackward];
}

-(void)keyPressed:(UIButton *)key
{
    [self.textDocumentProxy insertText:[key currentTitle]];
}

运行主应用程序并转到设置 - >常规 - >键盘 - >添加新键盘 - >并从第三方键盘部分添加键盘(显示的 keyboardName 将是 keyBoardCustom)

可以通过添加名为 Bundle display name 的键来更改键盘名称,并在值字符串值中输入主项目键盘的所需名称。

StackOverflow 文档

你还可以观看此 Youtube 视频