iOS 点击空白处收回键盘的几个简单代码

//收回键盘1

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

[self.view.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

[((UIView*)obj) resignFirstResponder];

}];

}

//收回键盘2

- (void)resignKeyBoardInView:(UIView *)view

{

for (UIView *v in view.subviews)

{

if ([v.subviews count] > 0)

{

[self resignKeyBoardInView:v];

}

if ([v isKindOfClass:[UITextView class]] || [v isKindOfClass:[UITextField class]]) {

[v resignFirstResponder];

}

}

}

//收回键盘3--

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

for (UIView* view in self.view.subviews) {

if ([view isKindOfClass:[UITextField class]])

    [view resignFirstResponder];

}

}

//收回键盘4---适用scrollview

//前提:view继承UIContorl,连线Touch Down

//Touch Down--Action

- (IBAction)backgroundTap:(id)sender

{

[phoneNumberTextFiled resignFirstResponder];//通知文本失去第一响应者状态 键盘收回

}

上一篇:6组-Alpha冲刺-6/6


下一篇:隐藏键盘的N种方法