touchesbegan for dismissing keyboard textview under scrollview not working - ios

I have a text field under a view which is under a scroll view inside a view controller which is one of the view controllers of a navigation controller. I want to dismiss the keyboard when the user touches outside the textfield. I tried using touchesBegan:, but it is not firing when it's under the scrollview. I also tried disabling user interaction in the scroll view, but nothing worked. Could anyone help me out?
func dismissKeyboard(txtField:UITextField)
{
txtField.resignFirstResponder()
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
dismissKeyboard(firstNameTxt)
}
Changed the below way for it to work
extension AddEditHireViewController {
func hideKeyboard()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: "dismissKeyboardNew")
self.formView.addGestureRecognizer(tap)
}
func dismissKeyboardNew()
{
self.formView.endEditing(true)
// self.dismissKeyboard(self.firstNameTxt)
} }

Whilst the solution given by Matthew Bradshaw works too, it will not dismiss the keyboard if the user scrolls. If you want it to do that, another solution would be to subclass UIScrollView. You could then override the touchesBegan method in this subclass, and then end editing accordingly.
class DismissableScrollView: UIScrollView {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
endEditing(false)
}
}
Once you have done this, simply change your existing UIScrollView's class to DismissableScrollView, or if you are already subclassing UIScrollView, you could add this to that subclass.

You could try to create an extension of the view controller. This has worked much smoother for me and with less hassle than trying to use .resignFirstResponder()
extension UIViewController
{
func hideKeyboard()
{
let tap: UITapGestureRecognizer = UITapGestureRecognizer(
target: self,
action: #selector(UIViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard()
{
view.endEditing(true)
}
}
Call self.hideKeyboard() in the viewDidLoad

Related

Can't dismiss keyboard when editing UITextView and touching anywhere outside of keyboard in table view controller

I have a couple textViews in one cell in a table view controller and I am trying to dismiss the keyboard when you touch anywhere outside the keyboard. I've tried the touches began method but it didn't work. The text views are not transparent and have user interaction enabled.
class RegisterTableViewController: UITableViewController {
override func viewDidLoad() {
// set all text views delegate to self
}
// dismiss keyboard on touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("touch")
super.touchesBegan(touches, with: event)
view.endEditing(true)
}
}
extension RegisterTableViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
textView.text = ""
}
}
I'm new to swift and would appreciate any help!
Add touchesBegan code in your UITableViewCell file , which will work if you touch outside TextField but inside cell
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.Your_TextField.endEditing(true)
}
But it won't work outside cell (In UIVIew of another ViewController) , so add UITapGesture to achieve that
override func viewDidLoad() {
super.viewDidLoad()
let tapgest = UITapGestureRecognizer(target: self, action: #selector(taptoend))
self.Your_Table_View.addGestureRecognizer(tapgest)
}
#objc func taptoend()
{
self.Your_Table_View.endEditing(true)
print("Key-Board will be dismissed here")
}
You need to add Tap gesture recognizer inside your cell. Place all you text inputs in a UIView. make outlet of UIView inside cell. and than add this code in your cell.
#IBOutlet weak var myView: UIView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
self.myView.addGestureRecognizer(tap)
}
#objc func dismissKeyboard() {
self.endEditing(true)
}

Preventing touch passing from UIControl to ParentView

I have created a custom control for Uber like OTP TextField. I want to consume the touch in my control and not let it propagate through the UIResponder Chain. So I have overridden the methods as described in apple documentation.
extension BVAPasswordTextField {
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
becomeFirstResponder()
}
override func touchesMoved(Set<UITouch>, with: UIEvent?) {
}
override func touchesEnded(Set<UITouch>, with: UIEvent?) {
}
override func touchesCancelled(Set<UITouch>, with: UIEvent?) {
}
override func touchesEstimatedPropertiesUpdated(Set<UITouch>) {
}
}
Now in some view controller I want to dismiss the keyboard when the user taps anywhere outside my custom control.
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecogniser = UITapGestureRecognizer(target: self, action: #selector(ViewController.backgroundTapped))
tapGestureRecogniser.numberOfTapsRequired = 1
view.addGestureRecognizer(tapGestureRecogniser)
// Do any additional setup after loading the view, typically from a nib.
}
#objc func backgroundTapped() {
self.view.endEditing(true)
}
But whenever I tap on the textfield backgroundTapped also gets called.
Note:- It is a control where based on enum values you can create different UI Components for taking input. So this control can be shared among the whole team... I won't be the only guy using it.... So I want it to behave exactly like UITextfield in touch scenario
You can handle the tap gesture using UITapGestureRecognizerDelegate. It allows you to decide whether or not gesture should begin. In your case, it should be done basing on the location of the touch.
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
// return true is location of touch is outside our textField
return !textField.frame.contains(location)
}
}
Just make sure you've set a delegate for your gesture at viewDidLoad
tapGestureRecogniser.delegate = self
UPDATE
If I got your setup right, you have a view and some subviews that are used for input. And you want to resignFirstResponder when this super-view is touched. In that case you can use gestureRecognizerShouldBegin like that
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
let location = gestureRecognizer.location(in: self.view)
for subview in self.view.subviews {
if subview.frame.contains(location) {
return false
}
}
return true
}
In my opinion, if you want to handle some view interaction in a specific way, you need to do it using that specific view. And what you're doing now feels like trying to change behavior on one view, using another view.

Calling function when user taps anywhere on screen

In my app I have a small menu I made which is basically a UIView with two button on it. The menu opens when the user taps a button and closes also when the user taps the same button. I'd like the menu to close when the user taps anywhere outside of the menu UIView.
The menu:
You can also apply this easy way
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapBlurButton(_:)))
self.view.addGestureRecognizer(tapGesture)
func tapBlurButton(_ sender: UITapGestureRecognizer) {
if //checkmenuopen
{
closemenuhere
}
}
For that when you show the small menu, add below it a invisible button (UIColor.clear) with the entire screen as a frame. And it's action is to dismiss the menu of yours.
Make sure when you dismiss the small menu to dismiss thus button as well.
Hope this helps!
You can use basically touches began function
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("TAPPED SOMEWHERE ON VIEW")
}
There are several solutions to your case:
1- Implementing touchesBegan(_:with:) method in your ViewController:
Tells this object that one or more new touches occurred in a view or
window.
Simply, as follows:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
// do something
}
2- Add a UITapGestureRecognizer to the main view of your ViewController:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doSomething(_:)))
view.addGestureRecognizer(tapGesture)
}
func doSomething(_ sender: UITapGestureRecognizer) {
print("do something")
}
Or -of course- you could implement the selector without the parameter:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(doSomething))
view.addGestureRecognizer(tapGesture)
}
func doSomething() {
print("do something")
}
3- You could also follow Mohammad Bashir Sidani's answer.
I would suggest to make sure add the appropriate constraints to your button whether it has been added programmatically or by storyboard.
I'm not sure the code below will work in your case, just a advice.
class ViewController: UIViewController {
var closeMenuGesture: UITapGestureRecognizer!
override func viewDidLoad() {
super.viewDidLoad()
closeMenuGesture = UITapGestureRecognizer(target: self, action: #selector(closeMenu))
closeMenuGesture.delegate = self
// or closeMenuGesture.isEnable = false
}
#IBAction func openMenu() {
view.addGestureRecognizer(closeMenuGesture)
// or closeMenuGesture.isEnabled = true
}
#IBAction func closeMenu() {
view.removeGestureRecognizer(closeMenuGesture)
// or closeMenuGesture.isEnabled = false
}
}
extension ViewController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
return touch.view === self.view // only valid outside menu UIView
}
}
And I never be in this situation so not sure making enable/disable closeMenuGesture is enough to ensure other controls work normally, or to add/remove closeMenuGesture is more insured.

iOS : recognize touch on UI scrollView

hi Im new to programming I was try to recognize the user touch on the screen and close all textfield but when I add a scrollview I won't be able to do that I read a lot in stack overflow like : tap recognizer but I could not do that anymore after adding the textfield I got confused
tell me how can I do it please?
thanks
for dismiss the keyboard is the subview of UIView, then use
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
for dismiss the keyboard is the subview of UIScrollview, then use in here scroll view observes the userInteraction so "By setting userInteractionEnabled to NO for your scroll view".
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.yourSCrollviewName.endEditing(true)
}
for resign the keyboard in various types, the some other type has already answered in SO
update
for hide the keyboard the create the TapGesture for your scrollview
self.ScrollView.isUserInteractionEnabled = true
// ScrollView.keyboardDismissMode = .onDrag
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 1
self.ScrollView.addGestureRecognizer(tap)
and call the action as
func doubleTapped() {
// do something cool here
self.ScrollView.endEditing(true)
}
You have to add tap gesture on ScrollView
Add below code in action of gesture:
self.yourSCrollviewName.endEditing(true)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(keyBoardHideOnTap))
tapGesture.numberOfTapsRequired = 1
self.ScrollView.addGestureRecognizer(tapGesture)
func keyBoardHideOnTap() {
// do something cool here
self.ScrollView.endEditing(true)
}

How to Dismiss keyboard when touching away across the entire app

I have multiple textFields in different viewControllers where keyboard is popped up.
I know how to dismiss keyboard when user clicks on a different part of the screen but I don't want to go and hard code it into every corner of my app.
So is there anyway to enforce keyboard getting keyboard dismissed everywhere on the app when the user clicks anywhere on the screen other than keyboard?
I was thinking of extending the UIViewController, but I also have some textFields inside a view that I add as a subview. Perhaps there could be someway that I extend TextField class itself?
I suggest to create a base UIViewController and let each of your ViewControllers inherit it; override touchesBegan method in it:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(true)
}
OR you can override viewDidLoad -in the base ViewController- and add a UITapGestureRecognizer to the view, as follows:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(Base.endEditing))
view.addGestureRecognizer(tapGesture)
}
func endEditing() {
view.endEditing(true)
}
You can also use an extension of a view controller, if you want the keyboard dismissal to apply to all of them:
extension UIViewController {
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}

Resources