萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS 文本框輸入時隱藏鍵盤的標準實現

核心原理:讓第一響應者辭職

所有收鍵盤操作本質都是:[textField resignFirstResponder]。本文針對「文本框輸入時隱藏鍵盤」這一具體場景,給出最標準實現。

標準流程:Return Key → Done → 代理方法

1. Storyboard 設定 Return Key 為 Done

  • 選中 `UITextField`
  • Attributes Inspector → **Return Key** → **Done**
  • (可選)勾選 **Auto-enable Return Key** — 內容為空時禁用

2. 實現 UITextFieldDelegate


// .h
@interface ViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UITextField *inputField;
@end

// .m
- (void)viewDidLoad {
    [super viewDidLoad];
    self.inputField.delegate = self;
}

// 核心代理方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES; // 返回 YES 讓系統處理換行等默認行為(Done 鍵無換行)
}

3. 連接 Did End On Exit(可選,Storyboard 專用)

若不想寫 Delegate,可在 Storyboard 將 Text Field 的 **Did End On Exit** 事件連接到:


- (IBAction)textFieldDoneEditing:(id)sender {
    [sender resignFirstResponder];
}

進階:數字鍵盤無 Done 鍵的解決方案

`UIKeyboardTypeNumberPad`、`PhonePad`、`DecimalPad` **沒有 Return Key**。

方案:自定義 inputAccessoryView 加 Done 工具列


- (void)addDoneToolbarToNumberPad:(UITextField *)textField {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
    toolbar.barStyle = UIBarStyleDefault;
    
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone 
                                                                              target:self 
                                                                              action:@selector(doneButtonTapped:)];
    toolbar.items = @[flexSpace, doneBtn];
    [toolbar sizeToFit];
    
    textField.inputAccessoryView = toolbar;
}

- (void)doneButtonTapped:(id)sender {
    [self.view endEditing:YES]; // 收起所有輸入框鍵盤
}

在 `viewDidLoad` 呼叫:`[self addDoneToolbarToNumberPad:self.numberField];`

通用工具類:一鍵為任意輸入框加上 Done 工具列


// UITextField+DoneToolbar.h
@interface UITextField (DoneToolbar)
- (void)addDoneToolbarWithTarget:(id)target action:(SEL)action;
@end

// UITextField+DoneToolbar.m
@implementation UITextField (DoneToolbar)
- (void)addDoneToolbarWithTarget:(id)target action:(SEL)action {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:target action:action];
    toolbar.items = @[flex, done];
    [toolbar sizeToFit];
    self.inputAccessoryView = toolbar;
}
@end

// 使用
[self.textField addDoneToolbarWithTarget:self action:@selector(dismissKeyboard)];
[self.numberField addDoneToolbarWithTarget:self action:@selector(dismissKeyboard)];

- (void)dismissKeyboard {
    [self.view endEditing:YES];
}

最佳實踐總結

  1. **有 Return Key 的鍵盤**:實現 `textFieldShouldReturn:` → `resignFirstResponder`
  2. **無 Return Key 的鍵盤**:加 `inputAccessoryView` Done 工具列 → `endEditing:YES`
  3. **複雜頁面多輸入框**:統一用 `endEditing:YES` 收起所有,避免逐個管理
  4. **ScrollView 頁面**:設定 `keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive` 獲得最佳體驗
copyright © 萬盛學電腦網 all rights reserved