How to hide the keyboard programmatically in iphone

后端 未结 6 2010
春和景丽
春和景丽 2021-01-11 13:43

How to hide the keyboard programmatically in iphone?

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-11 14:06

    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.

提交回复
热议问题