How to restrict UITextField to take only numbers in Swift?

后端 未结 30 938
难免孤独
难免孤独 2020-12-04 08:40

I want the user to only enter numeric values in a UITextField. On iPhone we can show the numeric keyboard, but on iPad the user can switch to any keyboard.

30条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 09:11

    IPhone only solution

    In whatever UITextField you're getting these values from, you can specify the kind of keyboard you want to appear when somebody touches inside the text field.

    E.G. a numeric-only keyboard.

    Like this screenshot:

    Ipad

    The iPad does not support the numeric keyboard, so your options are to either not support the iPad, validate the field post submit, or follow one of the other suggestions here to create same behaviors while running on an iPad.

提交回复
热议问题