Custom Keyboard In Xamarin.iOS

Introduction 

In this article, we are going to learn about how to create a simple custom keyboard in Xamarin ios.

Solution

Here are the steps to create a custom keyboard in Xamarin ios

Step 1 

Create your Xamarin iOS solution in Visual Studio/Xamarin Studio

Step 2  

Rigth click on Solution and choose Add --> Add New Project... 



Step 3 

Choose iOS --> Extension --> Custom Keyboard Extension and click Next.



Step 4
 

Set Extension Name as your need like "ACKeyBoard" and click next 



Step 5 

Now a window appears with your Projet Name and Solution Name, Click "Create".

 

Step 6 

After that, a new Extension Project is added to the existing project.

Inside the Extension project a class file is created with name KeyboardViewController that inherits UIInputViewController.

 

Step 7

In that keyboardViewController you can modify according to your own UI.
  1. public partial class KeyboardViewController: UIInputViewController {  
  2.     UIButton nextKeyboardButton;  
  3.     protected KeyboardViewController(IntPtr handle): base(handle) {  
  4.         // Note: this .ctor should not contain any initialization logic.  
  5.     }  
  6.     public override void DidReceiveMemoryWarning() {  
  7.         // Releases the view if it doesn't have a superview.  
  8.         base.DidReceiveMemoryWarning();  
  9.         // Release any cached data, images, etc that aren't in use.  
  10.     }  
  11.     public override void UpdateViewConstraints() {  
  12.         base.UpdateViewConstraints();  
  13.         // Add custom view sizing constraints here  
  14.     }  
  15.     public override void ViewDidLoad() {  
  16.         base.ViewDidLoad();  
  17.         // Perform custom UI setup here  
  18.         nextKeyboardButton = new UIButton(UIButtonType.System);  
  19.         nextKeyboardButton.SetTitle("Next Keyboard", UIControlState.Normal);  
  20.         nextKeyboardButton.SizeToFit();  
  21.         nextKeyboardButton.TranslatesAutoresizingMaskIntoConstraints = false;  
  22.         nextKeyboardButton.AddTarget(thisnew Selector("advanceToNextInputMode"), UIControlEvent.TouchUpInside);  
  23.         View.AddSubview(nextKeyboardButton);  
  24.         var nextKeyboardButtonLeftSideConstraint = NSLayoutConstraint.Create(nextKeyboardButton, NSLayoutAttribute.Left, NSLayoutRelation.Equal, View, NSLayoutAttribute.Left, 1.0 f, 0.0 f);  
  25.         var nextKeyboardButtonBottomConstraint = NSLayoutConstraint.Create(nextKeyboardButton, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, View, NSLayoutAttribute.Bottom, 1.0 f, 0.0 f);  
  26.         View.AddConstraints(new [] {  
  27.             nextKeyboardButtonLeftSideConstraint,  
  28.             nextKeyboardButtonBottomConstraint  
  29.         });  
  30.     }  
  31.     public override void TextWillChange(IUITextInput textInput) {  
  32.         // The app is about to change the document's contents. Perform any preparation here.  
  33.     }  
  34.     public override void TextDidChange(IUITextInput textInput) {  
  35.         // The app has just changed the document's contents, the document context has been updated.  
  36.         UIColor textColor = null;  
  37.         if (TextDocumentProxy.KeyboardAppearance == UIKeyboardAppearance.Dark) {  
  38.             textColor = UIColor.White;  
  39.         } else {  
  40.             textColor = UIColor.Black;  
  41.         }  
  42.         nextKeyboardButton.SetTitleColor(textColor, UIControlState.Normal);  
  43.     }  
  44. }  
Step 8 

Save and Run the project. In simulator you have to do some changes to add new custom keyboard. open simulator Go to Setting --> Genaral --> Keyboard --> Add New Keyboard.

 

Step 9

Inside Add New Keyboard, Select CustomKeyboard under Third-Party Keyboards that have installed. Now you can find Customkeyboard that created is added to keyboards.



Thus the newly generated keyboard is now added.


Keyboards can be switched by clicking "Next Keyboard".

 

Step 10 

To Remove added Keyboard choose Keyboards --> Edit. 

Up Next
    Ebook Download
    View all
    Learn
    View all