iOS collapse the keyboard

There are four code ways to close the keyboard in UIViewController:

1. Let the corresponding control give up the first responder

/* * resign first responder */ 
[ self.nameTextField  resignFirstResponder ];

2. Click anywhere on UIViewController to close the keyboard

- ( void )touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *) event {
     /* * Force end editing state */ 
    [ self.view  endEditing:YES ];
}

3. When it is difficult to obtain the current UIViewController, use UIApplication's method of abandoning the first responder

/* * Resign first responder */ 
[[ UIApplication sharedApplication ] sendAction:@selector( resignFirstResponder ) to:nil from:nil forEvent:nil];

4. When it is difficult to obtain the current UIViewController, use UIApplication's forced end editing method

/* * Force end editing state */ 
[[[UIApplication sharedApplication] keyWindow ] endEditing:YES ];

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324944932&siteId=291194637