2016-09-22 27 views
0

我正在爲Ipad開發一個應用程序。我正在設計一個忘記密碼的屏幕,允許用戶輸入密碼UITextField。按照設計,密碼只允許數字輸入。我可以在iPhone上設置UITextFiledkeyboardtypephonepad,但該選項似乎不適用於Ipad(Ipad總是顯示完整的鍵盤佈局)。我們如何才能實現只有號碼的iPad應用程序的鍵盤?UItextField限制用戶在ipad上輸入數字IOS應用程序

我必須設計自己的鍵盤佈局嗎? 任何幫助是非常感謝。謝謝!

回答

2

即使您使用只顯示數字的自定義鍵盤,用戶也可以始終粘貼某些東西或使用外部硬件鍵盤,但鍵盤類型並不指定文本字段接受的輸入類型。

要做到這一點,你需要觀察的輸入,例如,通過成爲UITextFieldDelegate然後:
例中迅速:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{ 
    // non decimal digit character set, better save this as a property instead of creating it for each keyboard stroke 
    let non_digits = NSCharacterSet.decimalDigits.inverted 
    // Find location for non digits 
    let range = string.rangeOfCharacter(from: non_digits) 
    if range == nil { // no non digits found, allow change 
     return true 
    } 
    return false // range was valid, meaning non digits were found 
} 

這將防止任何非數字字符被添加到文本框。

+0

感謝您的幫助。用於iPad應用程序中鍵盤的佈局。我們可以更改默認佈局(包含所有字符,數字符號的佈局)嗎? –

+0

@LêKhánhVinhiPad上沒有像iPad那樣的小數點佈局,我看到許多使用PIN碼的應用程序都會爲此使用自己的鍵盤。 –

+0

你知道任何庫或例子,以便我們可以按照實現ipad應用程序的數字鍵盤? –