您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 鍵盤控制:UIKeyboard 各項設定與遮擋問題解法

編輯:iOS 開發綜合

一、鍵盤類型

UITextField 與 UITextView 都透過 keyboardType 決定鍵盤配置,共八種可選:

typedef enum {
    UIKeyboardTypeDefault,                  // 預設,支援所有字元
    UIKeyboardTypeASCIICapable,             // 支援 ASCII 的預設鍵盤
    UIKeyboardTypeNumbersAndPunctuation,    // 電話鍵盤,含 * + # 等符號
    UIKeyboardTypeURL,                      // 網址鍵盤,帶 .com 快速鍵
    UIKeyboardTypeNumberPad,                // 純數字鍵盤
    UIKeyboardTypePhonePad,                 // 電話撥號鍵盤
    UIKeyboardTypeNamePhonePad,             // 電話鍵盤,亦可輸入人名
    UIKeyboardTypeEmailAddress,             // 電子郵件鍵盤,帶 @ 快速鍵
} UIKeyboardType;
textView.keyboardType = UIKeyboardTypeNumberPad;

二、鍵盤外觀

typedef enum {
    UIKeyboardAppearanceDefault,  // 預設淺色
    UIKeyboardAppearanceAlert,    // 深灰/石墨色,適合搭配警示樣式
} UIKeyboardAppearance;
textView.keyboardAppearance = UIKeyboardAppearanceDefault;

三、Return 鍵樣式

typedef enum {
    UIReturnKeyDefault,       // 灰色,標示 Return
    UIReturnKeyGo,            // Go
    UIReturnKeyGoogle,        // Google,用於搜尋
    UIReturnKeyJoin,          // Join
    UIReturnKeyNext,          // Next
    UIReturnKeyRoute,         // Route
    UIReturnKeySearch,        // Search
    UIReturnKeySend,          // Send
    UIReturnKeyYahoo,         // Yahoo!
    UIReturnKeyDone,          // Done
    UIReturnKeyEmergencyCall, // 緊急呼叫
} UIReturnKeyType;
textView.returnKeyType = UIReturnKeyGo;

四、自動大寫

typedef enum {
    UITextAutocapitalizationTypeNone,          // 不自動大寫
    UITextAutocapitalizationTypeWords,         // 每個單字首字母大寫
    UITextAutocapitalizationTypeSentences,     // 句首大寫
    UITextAutocapitalizationTypeAllCharacters, // 全部大寫
} UITextAutocapitalizationType;
textField.autocapitalizationType = UITextAutocapitalizationTypeWords;

五、自動更正

typedef enum {
    UITextAutocorrectionTypeDefault, // 預設
    UITextAutocorrectionTypeNo,      // 關閉
    UITextAutocorrectionTypeYes,     // 開啟
} UITextAutocorrectionType;
textField.autocorrectionType = UITextAutocorrectionTypeYes;

六、安全輸入

textView.secureTextEntry = YES;

用於密碼或敏感資料輸入時,開啟後會停用自動更正與輸入學習快取。

七、鍵盤遮住輸入框的解法

預設情況下鍵盤會蓋住下方的畫面。最直白的做法是:鍵盤彈起時把整個 self.view 往上推一個鍵盤高度的距離,收起時再移回來。

先設定代理:

textField.delegate = self;

接著實作兩個 delegate 方法,以動畫方式平移視圖:

// 開始編輯:視圖上移,讓出鍵盤空間
- (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];
}

// 按下 Return:視圖歸位並結束編輯
- (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 這個數字:

// 註冊通知
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

// 從 userInfo 中取出鍵盤最終的 frame,再換算需要位移的距離
- (void)keyboardWillShow:(NSNotification *)note {
    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat overlap = CGRectGetMaxY(self.view.bounds) - keyboardFrame.origin.y;
    // 依 overlap 決定位移量,並注意螢幕方向與 safe area
}

以通知搭配 Auto Layout 的 contentInset 調整,能同時兼容不同機型、不同鍵盤類型與橫向模式,也不受鍵盤高度變動影響。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved