1

I want to use .phonePad or .numberPad keyboard. But it has no return button. I need this.

How can I do it? I should create a custom keyboard?

Maiko Ohkawa
  • 853
  • 2
  • 11
  • 28

3 Answers3

2

You can add an UIToolbar above the keyboard using the UITextField's inputAccessoryView

Assuming you are using Swift :

func setupTextfield() {
    // ...
    myTextField.inputAccessoryView = inputAccessoryView()
    // ...
}

private func inputAccessoryView() -> UIToolbar {
    let keyboardToolbar = UIToolbar()
    keyboardToolbar.sizeToFit()
    keyboardToolbar.isTranslucent = false
    let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace,
                                        target: nil, action: nil)
    let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done,
                                        target: self, action: #selector(UIView.endEditing(_:)))
    keyboardToolbar.items = [flexBarButton, doneBarButton]
    return keyboardToolbar
}
Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
1

You can add toolbar at top of keyboard

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];

        toolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelAction)],
                             [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                             [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneAction)]];

        toolbar.barStyle = UIBarStyleBlackTranslucent;  
        [toolbar sizeToFit];
        yourTextField.inputAccessoryView = toolbar;
    }

    -(void)cancelAction{
        yourTextField.text = @"";
    }

    -(void)doneAction{
        NSString *text = yourTextField.text;
    }
Bilal L
  • 11
  • 3
1

You have to add toolbar on top of keyboard . Use InputAccessoryView property of textfield. here is complete tutorial for this

swift

https://iosdevcenters.blogspot.com/2016/01/how-to-adding-return-key-in-number.html

objective c

https://horseshoe7.wordpress.com/2012/08/17/tutorial-creating-a-done-button-on-the-iphone-number-pad-keyboard/

Hitesh Agarwal
  • 1,943
  • 17
  • 21