like hide keyboard when you play outside and inside a textfield when you have a scrollview

0

the problem is the following, I am developing an apps in the other view that I have achievement hide the keyboard perfectly but when I am using one with a scrollview I can not hide it in the others I use .resignFirstResponder() and it works perfect but here not nothing happens or error just does not hide the keyboard

import UIKit

class AreaOfAScaleneTriangleViewController: UIViewController {



    @IBOutlet var textfieldA: UITextField!
    @IBOutlet var textfieldB: UITextField!
    @IBOutlet var textfieldC: UITextField!




    @IBOutlet var label1: UILabel!
    @IBOutlet var label2: UILabel!
    @IBOutlet var label3: UILabel!
    @IBOutlet var label4: UILabel!
    @IBOutlet var label5: UILabel!

    @IBOutlet var semiPerimeterFormulaLabel: UILabel!

    override func viewDidLoad() {



        super.viewDidLoad()


        semiPerimeterFormulaLabel.text = "S = A + B + C / 2" // title semi-perimeter formula

        // Do any additional setup after loading the view.



    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




    @IBAction func calculate(_ sender: UIButton) {

        textfieldA.resignFirstResponder()
        textfieldB.resignFirstResponder()
        textfieldC.resignFirstResponder()

        let textFieldValorA = Double(textfieldA.text!)!
        let textFieldValorB = Double(textfieldB.text!)!
        let textFieldValorC = Double(textfieldC.text!)!

        label1.text = "\(textFieldValorA) + \(textFieldValorB) + \(textFieldValorC) / 2"

        let operation1 = textFieldValorA + textFieldValorB + textFieldValorC

        label2.text = "\(operation1)/2"


    }




    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        textfieldA.resignFirstResponder()
        textfieldB.resignFirstResponder()
        textfieldC.resignFirstResponder()
    }




}
    
asked by Liantony Pozo 04.08.2017 в 19:48
source

2 answers

2

I would like to make a couple of suggestions

The first is that instead of calling the resignFirstResponder() method in each UITextField, you could try the next call:

self.view.endEditing(true)

This has the disadvantage that it will iterate over any hierarchy of subViews .

The second one is for the tap part on scroll and do either resignFirstResponder() or self.view.endEditing(true) .

It takes a bit of context to see how you are "linking" the action to the ViewController , it would seem that you are doing it from InterfaceBuilder .

For this, you are looking for the Tap Gesture Recognizer element (it appears in the same bottom menu on the right side, where you can drag UILabels , UIButton etc).

Drag the Tap Gesture Recognizer and place it on scroll . In the menu on the left side, under your ViewController , it should have been added. You create an action , I prefer to do it with "drag control" from the Tap Gesture Recognizer to add it as a method to the ViewController . What I did to prove it looks like this:

@IBAction func scrollClick(_ sender: UITapGestureRecognizer) {
    self.view.endEditing(true)
}
    
answered by 09.08.2017 / 20:47
source
0

I hope this helps you. You can make this statement when you are loading the components that you are going to show: - Test in func viewDidLoad () ----

// *** Hide keyboard when tapping outside ***
            let tapGesture = UITapGestureRecognizer(target: self, action:     #selector(tapGestureHandler))
        view.addGestureRecognizer(tapGesture)

Then declare the function:

func tapGestureHandler() {
        inputTextView.endEditing(true)
  }

Where inputTextView is the TextView that implies that the keyboard appears or disappears.

    
answered by 17.08.2017 в 13:27