简体   繁体   中英

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 wich 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())
    }

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM