0

New to iPad development. I would like to use a custom numeric keyboard (using a pod) on iPad, but the native one on iPhones. Is it possible to make an extension on UITextField, UITextView to set/get inputView/keyboardType? Or is there an other solution?

myTextField.keyboardType = .decimalPad

extension UITextField {
    ....

    case .decimalPad:
       if UIDevice.current.userInterfaceIdiom == .pad {
    ...
}
Dan
  • 173
  • 2
  • 18

1 Answers1

0

You can set a custom input view using the inputView property of UITextField. If it is not set, the default keyboard (as configured on the text field) will appear when the text field becomes first responder. So only set if to your custom numeric keyboard view when running on iPad and leave it as-is on iPhone.

EDIT

Initially I pointed out inputAccessoryView instead of inputView, hence the confusion in the comments.

dr_barto
  • 5,723
  • 3
  • 26
  • 47
  • Thanks, could I override the inputAccessoryView property somehow without subclassing or do I need to set it for each UITextField in the app? (Without any changes the custom keyboard appears in the middle of the screen, using inputAccessoryView..) – Dan Feb 06 '18 at 10:07
  • My bad, you want `inputView` not `inputAccessoryView`... sorry for mixing them up. `inputView` actually replaces the keyboard. No need to subclass, just set a view to that property. – dr_barto Feb 06 '18 at 10:10
  • Thanks, wondering if there is a way to set the `inputView` property _once_ for all UITextFields in the app - "if iPad"? I suppose I have to make a subclass of UITextField, since the property cannot be overridden in an extension? – Dan Feb 08 '18 at 09:50
  • If you don't want to subclass you can add an extension with a static factory method a la `withPlatformSpecificKeyboard`, which encapsulates the platform-dispatching logic; you'd use this factory instead of the standard initializer to create `UITextField` instances. – dr_barto Feb 08 '18 at 11:21