鍵盤體驗是輸入類功能的成敗關鍵。UIKit 把鍵盤的行為拆成了幾個彼此獨立的屬性:鍵盤類型決定「能輸入什麼」,外觀決定「長什麼樣」,回車鍵決定「按下去代表什麼」,而自動大寫與自動更正決定「系統會不會幫你改字」。
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // 默認鍵盤,支持所有字符
UIKeyboardTypeASCIICapable, // 支持 ASCII 的默認鍵盤
UIKeyboardTypeNumbersAndPunctuation, // 數字與標點
UIKeyboardTypeURL, // 帶 .com 快捷鍵,適合網址輸入
UIKeyboardTypeNumberPad, // 純數字
UIKeyboardTypePhonePad, // 電話撥號鍵盤
UIKeyboardTypeNamePhonePad, // 適合輸入人名
UIKeyboardTypeEmailAddress, // 帶 @ 鍵,適合郵箱
UIKeyboardTypeDecimalPad // 帶小數點的數字鍵盤
};
textView.keyboardType = UIKeyboardTypeNumberPad;
typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, // 淺色
UIKeyboardAppearanceAlert, // 深灰 / 石墨色,用於提醒類輸入
UIKeyboardAppearanceDark // 深色
};
textView.keyboardAppearance = UIKeyboardAppearanceDefault;
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall
};
textView.returnKeyType = UIReturnKeyGo;
回車鍵的文案應當與當前語境匹配:搜索框用 Search,多步表單用 Next,最後一步用 Done。這種細節能顯著降低用戶的認知負擔。
typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone, // 不自動大寫
UITextAutocapitalizationTypeWords, // 每個單詞首字母大寫
UITextAutocapitalizationTypeSentences, // 每句首字母大寫
UITextAutocapitalizationTypeAllCharacters // 全部大寫
};
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;
typedef NS_ENUM(NSInteger, UITextAutocorrectionType) {
UITextAutocorrectionTypeDefault,
UITextAutocorrectionTypeNo,
UITextAutocorrectionTypeYes
};
textField.autocorrectionType = UITextAutocorrectionTypeNo;
輸入郵箱、密碼、驗證碼、代碼片段時,強烈建議關閉自動更正與自動大寫,否則系統會「好心」改掉用戶輸入,造成難以察覺的錯誤。
textField.secureTextEntry = YES;
開啟後鍵盤會禁用聯想與輸入緩存,並把字符顯示為圓點,專門用於密碼場景。
默認情況下鍵盤會直接蓋住下方的內容。一個簡單思路是:鍵盤彈起時把整個 self.view 上移一個鍵盤高度,鍵盤收起時再移回去。
先設置代理:
textField.delegate = self;
開始編輯時上移:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y -= 216;
frame.size.height += 216;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
按回車後移回原位並收起鍵盤:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSTimeInterval animationDuration = 0.30f;
CGRect frame = self.view.frame;
frame.origin.y += 216;
frame.size.height -= 216;
[UIView beginAnimations:@"ResizeView" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
把 216 硬編碼進代碼並不是好主意——不同設備、是否開啟輸入建議、鍵盤語言都會影響實際高度。正規方案是監聽鍵盤通知,從 userInfo 中取出真實的鍵盤 frame:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
- (void)keyboardWillShow:(NSNotification *)note {
CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 依據 keyboardFrame 與輸入框位置計算需要偏移的距離
}
另外,上文示例中的 beginAnimations 系列屬於老式寫法,新代碼應改用 UIView animateWithDuration: 或在 iOS 17 之後直接使用 UISpringTimingParameters。若界面元素較多,還可以考慮 IQKeyboardManager 這類成熟方案,它會自動處理約束調整與復雜嵌套場景。