Réponses:
Dans Swift:
Vous pouvez obtenir la hauteur du clavier en vous abonnant à la UIKeyboardWillShowNotification
notification. (En supposant que vous vouliez savoir quelle sera la hauteur avant qu'elle ne soit affichée).
Quelque chose comme ça:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: NSNotification.Name.UIKeyboardWillShow,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(keyboardWillShow),
name: UIResponder.keyboardWillShowNotification,
object: nil
)
Ensuite, vous pouvez accéder à la hauteur en keyboardWillShow
fonction comme celle-ci:
func keyboardWillShow(notification: NSNotification) {
let userInfo: NSDictionary = notification.userInfo!
let keyboardFrame: NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
}
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
UIResponder.keyboardWillShowNotification
dans le bit de nom
1- Enregistrez la notification dans la viewWillAppear
méthode:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
2- Méthode à appeler:
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
print(keyboardHeight)
}
}
keyboardWillShow
paramètre soit de type Notification
pour le rendre plus conforme à Swift 3.0.
viewDidLoad
n'est pas une bonne idée: où placez-vous l' removeObserver
appel correspondant pour que lorsque ce VC ne soit plus affiché, il cesse de recevoir des notifications? Il est préférable de mettre l'enregistrement pour les notifications viewWillAppear
, puis de mettre l' removeObserver
appelviewWillDisappear
Swift 4 et contraintes
À votre vue de table, ajoutez une contrainte inférieure par rapport à la zone de sécurité inférieure. Dans mon cas, la contrainte s'appelle tableViewBottomLayoutConstraint.
@IBOutlet weak var tableViewBottomLayoutConstraint: NSLayoutConstraint!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(notification:)), name: .UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillShow , object: nil)
NotificationCenter.default.removeObserver(self, name: .UIKeyboardWillHide , object: nil)
}
@objc
func keyboardWillAppear(notification: NSNotification?) {
guard let keyboardFrame = notification?.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else {
return
}
let keyboardHeight: CGFloat
if #available(iOS 11.0, *) {
keyboardHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
} else {
keyboardHeight = keyboardFrame.cgRectValue.height
}
tableViewBottomLayoutConstraint.constant = keyboardHeight
}
@objc
func keyboardWillDisappear(notification: NSNotification?) {
tableViewBottomLayoutConstraint.constant = 0.0
}
Mettre à jour Swift 4.2
private func setUpObserver() {
NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: UIResponder.keyboardWillShowNotification, object: nil)
}
méthode de sélection:
@objc fileprivate func keyboardWillShow(notification:NSNotification) {
if let keyboardRectValue = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardRectValue.height
}
}
extension:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:))
}
Mettre à jour Swift 3.0
private func setUpObserver() {
NotificationCenter.default.addObserver(self, selector: .keyboardWillShow, name: .UIKeyboardWillShow, object: nil)
}
méthode de sélection:
@objc fileprivate func keyboardWillShow(notification:NSNotification) {
if let keyboardRectValue = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardRectValue.height
}
}
extension:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(notification:))
}
Pointe
UIKeyboardDidShowNotification ou UIKeyboardWillShowNotification peut être appelé deux fois et obtenir un résultat différent, cet article explique pourquoi appelé deux fois.
Dans Swift 2.2
Swift 2.2 deprecates en utilisant des chaînes pour sélecteurs et introduit à la place nouvelle syntaxe: #selector
.
Quelque chose comme:
private func setUpObserver() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: .keyboardWillShow, name: UIKeyboardWillShowNotification, object: nil)
}
méthode de sélection:
@objc private func keyboardWillShow(notification:NSNotification) {
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let keyboardHeight = keyboardRectangle.height
editorBottomCT.constant = keyboardHeight
}
extension:
private extension Selector {
static let keyboardWillShow = #selector(YourViewController.keyboardWillShow(_:))
}
Version plus courte ici:
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = keyboardSize.height
}
}
Swift 4 .
Méthode la plus simple
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight : Int = Int(keyboardSize.height)
print("keyboardHeight",keyboardHeight)
}
}
override func viewDidLoad() {
// Registering for keyboard notification.
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}
/* UIKeyboardWillShowNotification. */
@objc internal func keyboardWillShow(_ notification : Notification?) -> Void {
var _kbSize:CGSize!
if let info = notification?.userInfo {
let frameEndUserInfoKey = UIResponder.keyboardFrameEndUserInfoKey
// Getting UIKeyboardSize.
if let kbFrame = info[frameEndUserInfoKey] as? CGRect {
let screenSize = UIScreen.main.bounds
//Calculating actual keyboard displayed size, keyboard frame may be different when hardware keyboard is attached (Bug ID: #469) (Bug ID: #381)
let intersectRect = kbFrame.intersection(screenSize)
if intersectRect.isNull {
_kbSize = CGSize(width: screenSize.size.width, height: 0)
} else {
_kbSize = intersectRect.size
}
print("Your Keyboard Size \(_kbSize)")
}
}
}
// Étape 1: - Enregistrer NotificationCenter
ViewDidLoad() {
self.yourtextfield.becomefirstresponder()
// Register your Notification, To know When Key Board Appears.
NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
// Register your Notification, To know When Key Board Hides.
NotificationCenter.default.addObserver(self, selector: #selector(SelectVendorViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
// Étape 2: - Ces méthodes seront appelées automatiquement lorsque le clavier apparaît ou se cache
func keyboardWillShow(notification:NSNotification) {
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
tblViewListData.frame.size.height = fltTblHeight-keyboardHeight
}
func keyboardWillHide(notification:NSNotification) {
tblViewListData.frame.size.height = fltTblHeight
}
La méthode de ZAFAR007 mise à jour pour Swift 5 dans Xcode 10
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight : Int = Int(keyboardSize.height)
print("keyboardHeight",keyboardHeight)
}
}
Je devais faire ça. c'est un peu du piratage. pas suggéré.
mais j'ai trouvé cela très utile,
j'ai fait une extension et une structure
Extension ViewController + Struct
import UIKit
struct viewGlobal{
static var bottomConstraint : NSLayoutConstraint = NSLayoutConstraint()
}
extension UIViewController{ //keyboardHandler
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
func listenerKeyboard(bottomConstraint: NSLayoutConstraint) {
viewGlobal.bottomConstraint = bottomConstraint
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
// Register your Notification, To know When Key Board Hides.
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
//Dismiss Keyboard
@objc func dismissKeyboard() {
view.endEditing(true)
}
@objc func keyboardWillShow(notification:NSNotification) {
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIResponder.keyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
UIView.animate(withDuration: 0.5){
viewGlobal.bottomConstraint.constant = keyboardHeight
}
}
@objc func keyboardWillHide(notification:NSNotification) {
UIView.animate(withDuration: 0.5){
viewGlobal.bottomConstraint.constant = 0
}
}
}
Utilisation:
obtenir la plus grande contrainte de fond
@IBOutlet weak var bottomConstraint: NSLayoutConstraint! // default 0
appeler la fonction dans viewDidLoad ()
override func viewDidLoad() {
super.viewDidLoad()
hideKeyboardWhenTappedAround()
listenerKeyboard(bottomConstraint: bottomConstraint)
// Do any additional setup after loading the view.
}
J'espère que cette aide.
-votre clavier se fermera automatiquement lorsque l'utilisateur tapera en dehors du champ de texte et
- il poussera toute la vue vers le clavier ci-dessus lorsque le clavier apparaîtra.
-vous pouvez également utiliser licenciementKeyboard () quand vous en avez besoin
J'utilise le code ci-dessous,
override func viewDidLoad() {
super.viewDidLoad()
self.registerObservers()
}
func registerObservers(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@objc func keyboardWillAppear(notification: Notification){
if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
self.view.transform = CGAffineTransform(translationX: 0, y: -keyboardHeight)
}
}
@objc func keyboardWillHide(notification: Notification){
self.view.transform = .identity
}