How to hide the keyboard programmatically in iphone

倾然丶 夕夏残阳落幕 提交于 2019-11-30 22:47:55

问题


How to hide the keyboard programmatically in iphone?


回答1:


Tell the UIResponder subclass that is currently first responder to resign its first responder status:

[responder resignFirstResponder];



回答2:


[textFieldName resignFirstResponder];



回答3:


It's easy:

ObjC

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

Swift

UIApplication.shared.keyWindow?.endEditing(true)

take a look at UIView Class Reference for endEditing. Causes the view (or one of its embedded text fields) to resign the first responder status. And keyWindow is the only window that receives keyboard events, so this solution is guarantied to always work.




回答4:


Call this in your ViewController

[self.view endEditing:YES];



回答5:


If you are using textview then

- (BOOL)textView:(UITextView *)textView
 shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
  if ([text isEqualToString:@"\n"])
 {
    [textView resignFirstResponder];
    [self keyboardWillHide];
 }
}

and if you are using textfield then

-(BOOL)textFieldShouldReturn:(UITextField*)textField;
 {

[textField resignFirstResponder];

 }



回答6:


Here is the swift version:

UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)


来源:https://stackoverflow.com/questions/2586937/how-to-hide-the-keyboard-programmatically-in-iphone

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