dismiss keyboard on tapping on any UIControl objects

ⅰ亾dé卋堺 提交于 2019-12-08 11:01:51

问题


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


回答1:


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];
  } 
}



回答2:


//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
    }
}



回答3:


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

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

    self.view.endEditing(true )

}




回答4:


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.



来源:https://stackoverflow.com/questions/30388641/dismiss-keyboard-on-tapping-on-any-uicontrol-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!