iosswiftuitextfielduipangesturerecognizerswrevealviewcontroller

UITextField resign first responder by using pan gesture recognizer for SWRevealViewController


I created a sidebar menu using the SWRevealViewController from GitHub. On my front View I have a textfield which becomes first responder by default prompting the keyboard to pop up. Now when I use the pan gesture to open the sidebar menu I need the textfield to resign as first responder and vice versa to become first responder again when the sidebar menu is closed by pan gesture.

Can this be done by using the pan direction?

Code used to implement the SWRevealViewController functionality into my project:

  if self.revealViewController() != nil {
        myButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.rightRevealToggle(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
    }

Solution

  • I had been working with/on SWRevealViewController for a while, then answering your question you can add your frontViewController as SWRevealViewControllerDelegate and then implementing this function

    func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition)
    

    you will be notified when frontViewController go to left or to front position

    this is the code

    in your frontViewController you need to add

    class FrontViewController: UIViewController, SWRevealViewControllerDelegate
     {
    @IBOutlet weak var textField: UITextField!
    
    override func viewDidLoad() {
    super.viewDidLoad()
    self.revealViewController().delegate = self;
     }
    

    and then

    func revealController(revealController: SWRevealViewController!, willMoveToPosition position: FrontViewPosition) {
    if(position == FrontViewPosition.Left)
    {
        self.textField.becomeFirstResponder();
    
    }else
    {
        self.textField.resignFirstResponder();
    }
    }
    

    I hope this help you!