Le comportement que vous décrivez est peut être accompli en manipulant la valeur booléenne isEnabled
si les éléments UI. Il y a quelques étapes pour rendre ceci facile et convivial.
Juste bonnes pratiques, et non requis pour cet exemple, mais je tiens à faire l'extension du MyViewController, inscrire/désinscrire aux notifications de clavier et ainsi de suite en elle, comme ceci:
import Foundation
import UIKit
extension MyViewController {
//MARK: Subscribing/Unsubribing to NotificationCenter.
func subcribeToKeyboardNotifications(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func unsubscribeToKeyboardNotifications(){
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
//MARK: Screen manipulation methods, to move the UIView up when the bottom text field is editing.
func keyboardWillShow(_ notification: Notification) {
if bottomTextField.isEditing {
view.frame.origin.y = getKeyboardHeight(notification) * -1
}
}
func keyboardWillHide(_ notification: Notification) {
if bottomTextField.isEditing {
view.frame.origin.y = 0
}
}
//MARK: Value returning method to calculate keyboard height.
private func getKeyboardHeight(_ notification: Notification) -> CGFloat {
let userInfo = (notification as NSNotification).userInfo!
let keyboardSize = userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.cgRectValue.height
}
}
Ensuite, Dans MyViewController, je règle le clavier sur resignToFirstResponder()
lorsque la touche de retour est appuyée, et je m'assure de régler mon UITextFieldDelegates
.
import Foundation
import UIKit
class MyViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var someButton: UIButton!
@IBOutlet weak var topTextField: UITextField!
@IBOutlet weak var bottomTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
//Set the delegates to self, it this example.
topTextField.delegate = self
bottomTextField.delegate = self
enableUIElements(true)
}
//Delegate function to type stuff into the text field.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
enableUIElements(false)
var newText = textField.text! as NSString
newText = newText.replacingCharacters(in: range, with: string) as NSString
let textSize: CGSize = newText.size(attributes: [NSFontAttributeName: textField.font!])
return textSize.width < textField.bounds.size.width
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder() //Dismiss the keyboard.
enableUIElements(true) //enable the UI elements.
return true
}
//Create a function to enable/disable UI elements.
func enableUIElements(_ enable: Bool){
someButton.isEnabled = enable
//TODO: add other elements here.
}
}
Notez que la dernière fonction !!! Utilisez-le pour activer/désactiver d'autres éléments lorsque le clavier est actif, comme je l'ai montré.
Cela devrait vous rendre où vous devez aller :)
Ne devrait pas être automatique, vous devez le coder comme ça, en désactivant par programme toutes les autres fonctionnalités. –
à droite. C'est ce que je me demande comment faire. Je sais userInteractionEnabled peut arrêter toute interaction mais je veux toujours que l'utilisateur puisse interagir avec le clavier – brownmamba
Par exemple, pour un bouton d'interface utilisateur, vous devriez faire yourButtonName.isEnabled = false –