許多輸入密集型 App(即時通訊、表單)會在鍵盤上方浮動一個「完成」按鈕,點擊收起鍵盤。難點在於鍵盤高度動態變化(中英切換、手寫、Emoji、第三方鍵盤),按鈕必須實時跟隨位置。
UIKeyboardWillShowNotification / UIKeyboardWillHideNotification(或統一用 UIKeyboardWillChangeFrameNotification)。userInfo 獲取鍵盤終態 Frame(螢幕坐標系)。- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = notification.userInfo;
CGRect keyboardEndFrame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = keyboardEndFrame.size.height;
CGFloat duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [info[UIKeyboardAnimationCurveUserInfoKey] intValue];
// 懶加載按鈕
if (!self.doneButton) {
self.doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.doneButton setImage:[UIImage imageNamed:@"keyboard_done"] forState:UIControlStateNormal];
[self.doneButton addTarget:self action:@selector(dismissKeyboard:) forControlEvents:UIControlEventTouchUpInside];
self.doneButton.frame = CGRectMake(0, 0, 60, 44); // 寬度自適應或固定
}
// 計算按鈕 Frame:右對齊,底部貼鍵盤頂部
CGRect btnFrame = self.doneButton.frame;
btnFrame.origin.x = self.view.bounds.size.width - btnFrame.size.width;
btnFrame.origin.y = self.view.bounds.size.height - keyboardHeight - btnFrame.size.height;
// 同步動畫
[UIView animateWithDuration:duration delay:0 options:curve << 16 animations:^{
if (self.doneButton.superview == nil) {
[self.view addSubview:self.doneButton]; // 或 [UIApplication sharedApplication].keyWindow
}
self.doneButton.frame = btnFrame;
self.doneButton.hidden = NO;
} completion:nil];
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *info = notification.userInfo;
CGFloat duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [info[UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView animateWithDuration:duration delay:0 options:curve << 16 animations:^{
self.doneButton.hidden = YES;
// 不要 removeFromSuperview 或 release!會導致動畫中途視圖消失崩潰
} completion:nil];
}
- (void)dismissKeyboard:(id)sender {
[self.view endEditing:YES]; // 遞歸讓所有子視圖 resignFirstResponder
}
self.view.bounds 計算相對位置最可靠。curve << 16 轉換為 UIViewAnimationOptions,保證按鈕與鍵盤同步移動。dealloc 時統一釋放,避免動畫過程中釋放導致 EXC_BAD_ACCESS。btnFrame.origin.y -= self.view.safeAreaInsets.bottom;更標準的做法是設置 UITextField.inputAccessoryView,系統自動管理顯示/隱藏/位置:
UIToolbar *accessory = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard:)];
accessory.items = @[flex, done];
self.textField.inputAccessoryView = accessory;
**優勢:零代碼管理位置、自動適配分屏/懸浮鍵盤、支持多輸入框共用/獨立配件。**
手動浮動按鈕適合高度定制化需求;標準場景**首選 inputAccessoryView**,零維護成本、系統級可靠性。