2012-06-05 37 views
24

Tôi cần kiểm soát, sau khi bàn phím được hiển thị và nhấn nút xong, khi ẩn bàn phím. Sự kiện nào được kích hoạt khi ẩn bàn phím trên iOS? Cảm ơn bạnSự kiện iOS khi bàn phím giấu

+0

http://developer.apple.com/library/ios/search /? q = bàn phím + ẩn –

+0

có thể trùng lặp của [ipad làm thế nào để biết bàn phím đã được ẩn] (http://stackoverflow.com/questions/7912246/ipad-how-to-know-keyboard-has-been-hidden) –

Trả lời

56

Có Sử dụng sau

//UIKeyboardDidHideNotification when keyboard is fully hidden 
//name:UIKeyboardWillHideNotification when keyboard is going to be hidden 

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil]; 

onKeyboardHide

-(void)onKeyboardHide:(NSNotification *)notification 
{ 
    //keyboard will hide 
} 
+1

Thao tác này sẽ kích hoạt tại thời điểm loại bỏ, không phải khi bàn phím bị ẩn hoàn toàn. –

+2

có, chính xác, vui lòng kiểm tra câu trả lời được cập nhật, để sử dụng thông báo ẩn hoàn toàn, hãy sử dụng 'UIKeyboardDidHideNotification' –

5

Bạn có thể nghe UIKeyboardWillHideNotification, nó được gửi bất cứ khi nào bàn phím bị loại bỏ.

+7

Để chính xác, thông báo được gửi TRƯỚC KHI bàn phím bị loại bỏ. –

+0

@Henri, đúng ... vì tôi đang xử lý vấn đề đó ngay bây giờ. – Morkrom

3

Nếu bạn muốn biết khi báo chí sử dụng nút Done, bạn phải thông qua giao thức UITextFieldDelegate, sau đó trong bạn Xem bộ điều khiển thực hiện phương pháp này:

Swift 3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    // this will hide the keyboard 
    textField.resignFirstResponder() 
    return true 
} 

Nếu bạn muốn biết chỉ đơn giản là khi bàn phím được hiển thị hoặc được che giấu, sử dụng một Notification:

Swift 3:

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: .UIKeyboardWillShow , object: nil) 

NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: .UIKeyboardWillHide , object: nil) 

func keyboardWillShow(_ notification: NSNotification) { 
    print("keyboard will show!") 

    // To obtain the size of the keyboard: 
    let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size 

} 

func keyboardWillHide(_ notification: NSNotification) { 
    print("Keyboard will hide!") 
} 
Các vấn đề liên quan