Chameleon
Chameleon

Reputation: 1608

dismiss keyboard on tapping on any UIControl objects

I have multiple keyboard provoking elements...UITextField, UITextView and UISearchBar.

I would like to dismiss UIKeyboard upon touch anywhere except keyboard and currently active "text editing" element.

I've implemented

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    searchBar.resignFirstResponder()
    name.resignFirstResponder() //UITextField
    notes.resignFirstResponder() //UITextView
    self.view.endEditing(true)
}

This works if user taps on an "inert" element...background or userDisabledView... but most of my View is made up of "active" elements like UITableView and UIButtons...

Is there a way to make this possible, regardless where the tap is made.


The only way I know of is to use a large invisible button that slides over the view whenever UIKeyboard is presented, which calls self.view.endEditing(true), then retracts to offscreen.

Any help?

NOTE: I resolved if tap is on UITableView, by implementing self.view.endEditing(true) in didSelectRowAtIndexPath, would still like to hear of other methods for the rest

Upvotes: 0

Views: 445

Answers (4)

Subhash Sharma
Subhash Sharma

Reputation: 745

you want to dismiss keybord on active controls when they tapped, for that you have to use

resignFristResponder()

on every active controls like if you have a UIButton than you have to put this method on buttonClick Action. like ex-

  @IBAction func nextButtonTapped(sender: AnyObject)
{

    txtlang.resignFirstResponder()
}

its only example. Hope it helps you

edit 1 = you can also use endEditing() method as it is. **my english is very poor. sorry for that.

Upvotes: 1

Irshad Qureshi
Irshad Qureshi

Reputation: 819

This Always worked for me.....!!

override func touchesBegan(touches: Set, withEvent event: UIEvent) {

    self.view.endEditing(true )

}

Upvotes: 1

酷酷的哀殿
酷酷的哀殿

Reputation: 1031

//currently active "text editing" element.
var firstResponderView : UIView?

var tap: UITapGestureRecognizer?

//cover the currently active "text editing" element.
var smallView:UIView?

//dont't cover the keyboard
var bigView: UIView?

func createView(){
    bigView = UIView(frame: CGRectZero)
    bigView!.userInteractionEnabled=false

    tap = UITapGestureRecognizer(target: self, action: "endEditing")

    smallView = UIView(frame: CGRectZero)
    smallView!.userInteractionEnabled=false
    bigView!.addSubview(smallView!)
    self.view.addSubview(bigView!)
    self.update()
}

func endEditing(){
    self.view.endEditing(true)
    self.update()
}

//call this after any view become first responder or resign
func update() {

    if(firstResponderView?.isFirstResponder()==true)
    {
        bigView!.addGestureRecognizer(tap!)
        bigView!.userInteractionEnabled = true
        smallView!.frame = firstResponderView!.frame
    }
    else{
        bigView!.removeGestureRecognizer(tap!)
        bigView!.userInteractionEnabled = false
        smallView!.frame=CGRectZero
    }
}

Upvotes: 1

Vizllx
Vizllx

Reputation: 9246

You can attach tap gesture for every controls inside the view, tapGestRecog.cancelsTouchesInView=NO prevents the tap recognizer to be the only one to catch all the taps then resign your keyboard on tapAction.

First in your viewDidLoad, add keyboard notification:-

// Listen for keyboard appearances and disappearances
[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidShow:)
                                             name:UIKeyboardDidShowNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardDidHide:)
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

Now set a BOOL value , to set a value whether keyboard is present or not

- (void)keyboardDidShow: (NSNotification *) notif{
     isKeyboardPresent=TRUE;
    // Do something here
}

- (void)keyboardDidHide: (NSNotification *) notif{
     isKeyboardPresent=False;
    // Do something here
}

Now, add tap gesture to all your controls

for(UIView *vw in [self.view subviews])
     {
         UITapGestureRecognizer *tapGestRecog = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(controlTapAction)];
         tapGestRecog.delegate=self;
         [tapGestRecog setNumberOfTapsRequired:1];
         tapGestRecog.cancelsTouchesInView = NO;
         [vw addGestureRecognizer:tapGestRecog];

     }
-(IBAction) controlTapAction
{
  if(isKeyboardPresent)
   {
       [self.view endEditing:TRUE];
  } 
}

Upvotes: 0

Related Questions