Как определить, когда клавиатура показана и скрыта
Как я могу определить, когда клавиатура отображается и скрыта от моего приложения?
11 ответов
В методе ViewDidLoad вашего класса, настроенном на прослушивание сообщений о клавиатуре:
// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
Тогда в указанных вами методах (в данном случае keyboardDidShow
а также keyboardDidHide
) с этим можно что-то сделать
- (void)keyboardDidShow: (NSNotification *) notif{
// Do something here
}
- (void)keyboardDidHide: (NSNotification *) notif{
// Do something here
}
Вы можете просто нуждаться addObserver
в viewDidLoad
, Но имея addObserver
в viewWillAppear
а также removeObserver
в viewWillDisappear
предотвращает редкие сбои, которые происходят при смене вида.
Swift 4.2
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillAppear() {
//Do something here
}
@objc func keyboardWillDisappear() {
//Do something here
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Свифт 3 и 4
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear), name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear() {
//Do something here
}
@objc func keyboardWillDisappear() {
//Do something here
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Олд Свифт
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillAppear(notification: NSNotification){
// Do something here
}
func keyboardWillDisappear(notification: NSNotification){
// Do something here
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
Свифт 3:
NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(viewController.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keyboardWillShow(_ notification: NSNotification){
// Do something here
}
func keyboardWillHide(_ notification: NSNotification){
// Do something here
}
Swift 5
Выше указаны правильные ответы. Хотя я бы предпочел создать помощник, чтобы завершитьnotification's observers
.
Преимущество:
- Вам не нужно повторять каждый раз, когда вы управляете поведением клавиатуры.
- Вы можете расширить другое уведомление, реализовав другое значение перечисления
- Это полезно, когда вам приходится иметь дело с клавиатурой в нескольких контроллерах.
Образец кода:
extension KeyboardHelper {
enum Animation {
case keyboardWillShow
case keyboardWillHide
}
typealias HandleBlock = (_ animation: Animation, _ keyboardFrame: CGRect, _ duration: TimeInterval) -> Void
}
final class KeyboardHelper {
private let handleBlock: HandleBlock
init(handleBlock: @escaping HandleBlock) {
self.handleBlock = handleBlock
setupNotification()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setupNotification() {
_ = NotificationCenter.default
.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: .main) { [weak self] notification in
self?.handle(animation: .keyboardWillShow, notification: notification)
}
_ = NotificationCenter.default
.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: .main) { [weak self] notification in
self?.handle(animation: .keyboardWillHide, notification: notification)
}
}
private func handle(animation: Animation, notification: Notification) {
guard let userInfo = notification.userInfo,
let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double
else { return }
handleBlock(animation, keyboardFrame, duration)
}
}
Как пользоваться:
private var keyboardHelper: KeyboardHelper?
...
override func viewDidLoad() {
...
keyboardHelper = KeyboardHelper { [unowned self] animation, keyboardFrame, duration in
switch animation {
case .keyboardWillShow:
print("keyboard will show")
case .keyboardWillHide:
print("keyboard will hide")
}
}
}
Свифт 4:
NotificationCenter.default.addObserver( self, selector: #selector(ControllerClassName.keyboardWillShow(_:)),
name: Notification.Name.UIKeyboardWillShow,
object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ControllerClassName.keyboardWillHide(_:)),
name: Notification.Name.UIKeyboardWillHide,
object: nil)
Затем добавляем метод, чтобы остановить прослушивание уведомлений, когда жизнь объекта заканчивается:-
Then add the promised methods from above to the view controller:
deinit {
NotificationCenter.default.removeObserver(self)
}
func adjustKeyboardShow(_ open: Bool, notification: Notification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
let height = (keyboardFrame.height + 20) * (open ? 1 : -1)
scrollView.contentInset.bottom += height
scrollView.scrollIndicatorInsets.bottom += height
}
@objc func keyboardWillShow(_ notification: Notification) {
adjustKeyboardShow(true, notification: notification)
}
@objc func keyboardWillHide(_ notification: Notification) {
adjustKeyboardShow(false, notification: notification)
}
В Swift 4.2 имена уведомлений перемещены в другое пространство имен. Так что теперь его
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
addKeyboardListeners()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
func addKeyboardListeners() {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc private extension ClassName {
func keyboardWillShow(_ notification: Notification) {
}
func keyboardWillHide(_ notification: Notification) {
}
}
Свифт - 4
override func viewWillAppear(_ animated: Bool) {
addKeyBoardListener()
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self) //remove observer
}
func addKeyBoardListener() {
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil);
}
@objc func keyboardWillShow(_ notification: Notification) {
}
@objc func keyboardWillHide(_ notification: Notification) {
}
Обратитесь к разделу " Управление клавиатурой " в "Руководстве по программированию текста, Интернета и редактирования" для получения информации о том, как отслеживать отображаемую или скрытую клавиатуру, а также о том, как отображать / отклонять ее вручную.
Вы хотите зарегистрироваться для 2 клавиатурных уведомлений:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
Отличный пост о том, как настроить TextField на клавиатуре - http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html
Swift 4 - dd 20 october 2017
override func viewDidLoad() {
[..]
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
}
@objc func keyboardWillAppear(_ notification: NSNotification) {
if let userInfo = notification.userInfo,
let keyboardFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue).cgRectValue {
let inset = keyboardFrame.height // if scrollView is not aligned to bottom of screen, subtract offset
scrollView.contentInset.bottom = inset
scrollView.scrollIndicatorInsets.bottom = inset
}
}
@objc func keyboardWillDisappear(_ notification: NSNotification) {
scrollView.contentInset.bottom = 0
scrollView.scrollIndicatorInsets.bottom = 0
}
deinit {
NotificationCenter.default.removeObserver(self)
}
Если у вас есть более одного UITextField
Если вам нужно что-то сделать, когда (или до) клавиатура появляется или исчезает, вы можете реализовать этот подход.
добавлять UITextFieldDelegate
в ваш класс. Назначьте счетчик целых чисел, скажем:
NSInteger editCounter;
Установите этот счетчик на ноль где-нибудь в viewDidLoad
, Затем реализовать textFieldShouldBeginEditing
а также textFieldShouldEndEditing
делегировать методы.
В первом добавьте 1 к editCounter. Если значение editCounter становится равным 1 - это означает, что появится клавиатура (в случае, если вы вернете YES). Если editCounter > 1 - это означает, что клавиатура уже видна, а другой UITextField удерживает фокус.
В textFieldShouldEndEditing
вычтите 1 из editCounter. Если вы получите ноль - клавиатура будет отклонена, в противном случае она останется на экране.
Вы можете использовать библиотеку KBKeyboardObserver. Он содержит несколько примеров и предоставляет простой интерфейс.
Существует какао-капсула для облегчения наблюдения за NSNotificationCentr
для видимости клавиатуры здесь: https://github.com/levantAJ/Keyhi
pod 'Keyhi'
Так что теперь это настоящий ответ.
import Combine
class MrEnvironmentObject {
/// Bind into yr SwiftUI views
@Published public var isKeyboardShowing: Bool = false
/// Keep 'em from deallocatin'
var subscribers: [AnyCancellable]? = nil
/// Adds certain Combine subscribers that will handle updating the
/// `isKeyboardShowing` property
///
/// - Parameter host: the UIHostingController of your views.
func setupSubscribers<V: View>(
host: inout UIHostingController<V>
) {
subscribers = [
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillShowNotification)
.sink { [weak self] _ in
self?.isKeyboardShowing = true
},
NotificationCenter
.default
.publisher(for: UIResponder.keyboardWillHideNotification)
.sink { [weak self, weak host] _ in
self?.isKeyboardShowing = false
// Hidden gem, ask me how I know:
UIAccessibility.post(
notification: .layoutChanged,
argument: host
)
},
// ...
Profit
.sink { [weak self] profit in profit() },
]
}
}