Problème avec un textField, clavier persistant

C’est un textField banal, standard, à côté d’un autre. Voilà qu’en pressant la touche “return” le clavier se refuse à disparaître dans les conditions standard de température et de programmation.

Voilà ce que dit la console:

-[UIWindow endDisablingInterfaceAutorotationAnimated:] called on <UIRemoteKeyboardWindow: 0x7fe412517690; frame = (0 0; 320 568); opaque = NO; autoresize = W+H; layer = <UIWindowLayer: 0x6180000304c0>> without matching -beginDisablingInterfaceAutorotation. Ignoring.

Ignoring, ça je le constate. Quelqu’un a une idée de ce que je peux faire ?

Hello,

Tu peux nous mettre ton code associé à ton textField ?

dans le viewDidLoad :
…data1_name.delegate = self
data2_name.delegate = self…
dans les déclarations d’outlet :
@IBOutlet weak var data1_name: UITextField!
@IBOutlet weak var data2_name: UITextField!
override func viewWillAppear(_ animated: Bool) {

    // remise à zéro de l'écran
    annulerSaisieCancelPressed(Any.self)
    // préparation pour le clavier
    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)
}
func keyboardWillShow() -> String{

// à l’arrache, la taille du clavier en hauteur
self.view.frame.origin.y = -150
return “keyboardWillShow”
}
func keyboardWillHide() -> String{
self.view.frame.origin.y = 0
return “keyboardWillHide”
}

…et aussi :
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == data1_name{
self.data1_name.endEditing(true)
}
else {
self.data2_name.endEditing(true)
}
return true
}
…et encore :
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

et quelques autres participations des textField consistant à leur piquer leur texte, ce qui marche par conte sans poser problème.

Hello,

Et si tu rajoutes

textField.resignFirstResponder()

Afin d’indiquer à ton textField de lâcher la main sur l’édition du champ

1 « J'aime »

N’est-ce pas déjà fait ? voici la documentation de EndEditing :
func endEditing(_ force: Bool) -> Bool
Description
Causes the view (or one of its embedded text fields) to resign the first responder status.
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to true, the text field is never even asked; it is forced to resign.
Parameters
force
Specify true to force the first responder to resign, regardless of whether it wants to do so.
Returns
true if the view resigned the first responder status or false if it did not.
SDKs iOS 2.0+, tvOS 9.0+

Bonjour.

jacquemin5h
Si je comprends bien ce que je lis, ce n’est pas la mesure du clavier qui justifie le mouvement, mais celle du textField à ne pas recouvrir ?

Effectivement, une autre possibilité, sans employer les Notification.
Si tu copie cette routine et que tu change tout les movetf par movetv
et tout les textField par textView tu as deux routine qui controle ton
clavier au complet.:slightly_smiling_face:

Je ne vois pas comment éviter les Notification. Il y a des non-dits dans cette routine. On la complète ?

import UIKit

class ViewController: UIViewController {
var keyH: Int = 0
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.getKeyh(notification: )), name: NSNotification.Name.UIKeyboardDidChangeFrame, object: nil)
// Do any additional setup after loading the view, typically from a nib.
}
@objc func getKeyh(notification: NSNotification){
let info : NSDictionary = notification.userInfo! as NSDictionary
keyH = Int(((info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size.height)!)

}
func textFieldDidBeginEditing(_ textView: UITextView) {
    let tfInfo : CGRect = self.view.window!.convert(textView.bounds,from: textView)
    let screenHeight = Int( UIScreen.main.bounds.size.height)
    var moveTf = screenHeight - (Int(tfInfo.minY))  - (Int(tfInfo.height))
    print("\n\(moveTf) et keyH=\(keyH) and diff=")
    
    
    if moveTf < keyH {
        moveTf = moveTf - keyH
        print("\(moveTf)\n")
        let moveDuration = 0.3
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(moveDuration)
        view.frame = view.frame.offsetBy(dx: 0, dy: CGFloat(moveTf))
        UIView.commitAnimations()
        textView.autocorrectionType = UITextAutocorrectionType.no
        super.viewDidAppear(true)
    } else {
        moveTf = 0
    }
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

Bonjour a toi.
Quand on utilise une
func textFieldDidBeginEditing
C’est que dans ta classe on utilise delegate.
. . . . . . . . . . . . . . . . . . . . . . . . .
Je te donne plus d’ information.
voici la routine pour KeyH
, , , , , , ,
texte override func viewWillLayoutSubviews() {
screenHeight = Int(UIScreen.main.bounds.height)
screenWidth = Int(UIScreen.main.bounds.width)
keyH = 222 // was 228
if screenWidth > 1000 || screenHeight > 1000 {
keyH = 400 // was 315
}
if screenWidth > 1350 || screenHeight > 1350 {
keyH = 470 // was 380
}
}
, , , , , , ,
// les var
var moveTf = 0
var moveTv = 0
var keyH = 100
var screenHeight = 0
var screenWidth = 0
, , , , , , , ,
Voici le problem, tu doit utiliser les delegate
si tu n’utilise pas les notifications

// Exp: ta classe doit avoir.

class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate {

// très simple ajoute les textfield et si tu veut les textView pas
// nécessaire pour tes premier test.
//et maintenant tu doit utiliser cette class exp:

override func viewDidLoad() {
super.viewDidLoad()

    tfList4.delegate = self  // ou self.tfList4.delegate = self
    tfList5.delegate = self
    tfList6.delegate = self
    tfList7.delegate = self

// voici je que j’aime comme tu voie je n’ai
// pas utiliser tfList1.delegate = self
// pour t’aider fait quelque test.
// seulement ceux que tu veut qui bouge
// ceux la tu les mets avec delegate
// si tfList1.delegate est completement en haut
// tu na pas a le faire bougé. Donc tu peut ne
// pas le mettre dans ta liste
// j’ai des app qui ont plus de 20 textfield
// et seulement 7 ou 8 que je veut utiliser
// du au clavier.

//Maintenant pour finir voici la routine pour
// remettre ton textField a sa place original
// toujours sans notification.

func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    if moveTf != 0 {
        self.view.frame = self.view.frame.offsetBy(dx: 0, dy: CGFloat(-moveTf))
        UIView.commitAnimations()
    }
    moveTf = 0
    return true
    
}

Voici une autre routine tres agreable pour le clavier quand
return ne veut pas fonctionner. Avec cette routinre tu tap
ou tu veut sur l’écran et le clavier s’enlève.

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

PS. noublie pas d’ajouté la routine que je t’ai envoyer.
func textFieldDidBeginEditing(_ textView: UITextView) {
Maintenant a toi de t’amuser :hugs: