Objective-c Code Instance mode one
[[[UIApplication sharedApplication]keyWindowendEditing:YES];
If there are many textfield on a view, you can directly
[self.view endEditing:YES];
Objective-c Code Instance mode two
[[ SelfFindfirstresponderbeneathview: Self] Resignfirstresponder]; [[ SelfFindfirstresponderbeneathview: Self] Becomefirstresponder];//Open Keyboard- (UIView*) Findfirstresponderbeneathview: (UIView*) View {//Search recursively for first responder for(UIView*childview in view. Subviews) {if([Childview Respondstoselector:@selector(Isfirstresponder)] && [Childview Isfirstresponder])returnChildview;UIView*result = [ SelfFindfirstresponderbeneathview:childview];if(Result)returnResult }return Nil;}
Swift Mode One
UIApplication.sharedApplication().keyWindow?.endEditing(true)
Swift Mode II
func findFirstResponderBeneathView(view: UIView) -> () { forin view.subviews { let isChildFirstResponder = childView.isFirstResponder() let isChildResponderSelector = childView.respondsToSelector("isFirstResponder") if isChildFirstResponder && isChildResponderSelector { childView.resignFirstResponder() } self.findFirstResponderBeneathView(childView as! UIView) } }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS off keyboard simple implementation (Objc/swift)