Custom KeyBoard Example
suggest changeObjective-C and Xib
Add a target to an existing XCode project
![](/essential/ios/img/b5912f18fd710bef5af0cb5328373de2a7b3a162.png)
In the Add Target select Custom KeyBoard
![](/essential/ios/img/f25c8959bc0c7fc952a0387f75a7cd2e3d629e02.png)
Add the target like this:
![](/essential/ios/img/4a1ddd6bf52c47c901a2912c7721991af3cee57d.png)
Your project file directory should look something like this
![](/essential/ios/img/bd980d174fd2c209b2b0d53bc766b430af300bc9.png)
Here myKeyBoard is the name of the added Target
Add new Cocoatouch file of type of type UIView and add an interface file
![](/essential/ios/img/7b045e320c584bcd37d4e2490d1af9b9f3944438.png)
Finally your project directory should look like this
![](/essential/ios/img/16eb419703f38635ec5b83ed80eae3fe1a1d9f88.png)
make the keyBoardView.xib
a subclass of keyBoardView
![](/essential/ios/img/9649203384dfc0eb5d9d748c02712e99a007e1d7.png)
Make interface in the keyBoardView.xib
file
![](/essential/ios/img/65c8679e9f148ff06aea802f0e69e1593126f9dd.png)
Make connections to from the keyBoardView.xib
to keyBoardView.h
file
keyBoardView.h
should look like
#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
In the keyBoardViewController.h
file import #import "keyBoardView.h"
Declare a property for keyboard @property (strong, nonatomic)keyBoardView *keyboard;
Comment out the
@property (nonatomic, strong) UIButton *nextKeyboardButton and all the code associated with it
The KeyboardViewController.m file’s viewDidLoad() function should look like this
- (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;
}
The functions addGestureToKeyboard
, pressDeleteKey
, keyPressed
are defined below
-(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]];
}
Run the Main Application and go to Settings->General->Keyboard->Add New Keyboard-> and add keyboard from the third party keyboard section (The displayed keyboardName would be keyBoardCustom)
The keyboard name can be changed by adding a key called Bundle display name
and in the Value String Value enter the desired name for the keyboard of the main Project.
![](/essential/ios/img/93cc8105cc785af55994e5b8611801b1a8d3a058.png)
You can also watch this Youtube Video