In my case, I am having two textview one I placed into view another one within UIAlertController. Here, I added done and cancel button on keyboard accessory with actions. Now, how to create resign responder for both UITextView?
#IBAction func doneClick(_ sender: Any) {
self.descriptionTextView.resignFirstResponder()
self.textView.resignFirstResponder() // Its making crash sometime
}
It appears that one of them is nil at some time , so You can do
self.view.endEditing(true)
Or make them optional like
self.descriptionTextView?.resignFirstResponder()
self.textView?.resignFirstResponder()
Related
I have changed my view to a UIControl in order to add a TouchDown recognizer (that way I can dismiss isEditing in my UITextView). I have also added an outlet to a function in my class. All my other outlets work, but when I touch down this never gets called. Could it be because my Control has many things layered on top of it? Any other idea why?
#IBAction func userTappedBackground(_ sender: Any) {
bioTextView.endEditing(true)
}
I am trying to implement a very basic chat feature into my app and I am using constraints to keep everything in the correct place. It is great except for when I need to actually type, and the problem that arises is that the keyboard covers the text field and I not only cannot see the textfield but I cannot dismiss it. Thank you for all help!
In summary,
Using a textfield with contraints at bottom of screen
keyboard shows up and covers it, and I cannot dismiss the keyboard
Just set observers for UIKeyboardWillShowNotification and UIKeyboardWillHideNotification.
Whenever, UIKeyboardWillShowNotification is triggered, move the UITextfield upwards equivalent to the keyboard height. Then, when the UIKeyboardWillHideNotification is triggered, move the keyboard back into place.
Dismiss keyboard by tapping anywhere
override func viewDidLoad()
{
super.viewDidLoad()
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)}
func dismissKeyboard()
{
view.endEditing(true)
}
All UIViewControllers in my app are managed by a top level UINavigationController. In the UIViewController that is currently on top of my navigation stack I have a set of UITextFields.
A problem occurs when I call becomeFirstResponder() on one of these text fields and then immediately navigate back without changing focus first, e.g. by tapping on another field. After navigating back one level, the keyboard appears, and I have found no way to keep it from appearing or making it disappear. It even stays as I further push views of the navigation stack.
This is directly or indirectly connected to the
becomeFirstResponder() call, because without that call, the
problem does not occur.
Even if I, for test purposes, call
resignFirstResponder() immediately after becomeFirstResponder()
the keyboard still appears after navigating back.
I have tried other ways like detecting and resigning the first responder or calling endEditing() in viewWillDisappear() but did not succeed. I’m not even sure what this keyboard belongs to after the corresponding view is popped off the stack. I cannot inspect the keyboard in the View Debugger as it does not appear there.
Why does the keyboard appear, and how can I prevent it?
It turns out that the form validation that was grabbing the first responder kept reclaiming it until the field content was valid. If it does not release the status before back navigation the keyboard stays and it becomes difficult to assign first responder to another control.
Solution in my case was to more carefully keep track of which field is first responder, detect the back button push, allow resigning first responder unconditionally in that case, and then resign first responder for that field.
var currentTextField: UITextField?
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if let currentField = self.currentTextField {
currentField.resignFirstResponder()
}
}
override public func willMoveToParentViewController(parent: UIViewController?) {
if (parent == nil) {
backButtonPushed = true
}
super.willMoveToParentViewController(parent)
}
func customTextFieldDidBeginEditing(textField: UITextField) {
currentTextField = textField
}
public func textFieldShouldEndEditing(textField: UITextField) -> Bool {
// ...
// Must return true if back button is pushed.
if backButtonPushed {
return true
} else {
// ...
}
}
Did you try to call endEditing() in viewWillAppear() of the new VC instead?
Im sure this is simple but I can't figure it out so I thought I'd ask! I have a tab view controller with 5 different tabs on it. On one of them, when somebody clicks on it, I want the cursor to be automatically selected in the textfield so they can start typing right away. How can I do this in Swift?
You can select a text field with
textfield.becomeFirstResponder()
You'd probably want to put that in you viewWillAppear or viewDidAppear
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
textField.becomeFirstResponder()
}
Is there any way to assign a segue as well as an IBAction to a button? Also would there be a different method of attempting this of transitioning from an MPMediaPickerController (e.i, the user would be on one view controller, choose a song, and appear in another)
The easiest way of doing that would be to place the segue into the IBAction. That way it will run both. To keep your code clean you could create a separate method for the segue and then call that method from inside the IBAction.
Example
#IBAction func buttonPressed(sender: UIButton) {
segue()
//do stuff
}
func segue() {
//run the transition
}
Hope that helps :)