【原】触摸背景关闭键盘的代码实现

//把bgView强制修改成UIControl,以便响应触摸事件
//UIControl是UIView的子类

//其实有很多情况都可以这样用,用一个普通的UIView来模拟某些控件,可以很灵活!!!!

 

UIView *bgView;


CGRect bgFrame = CGRectMake(0.0, 35.0, 320.0, 460.0 - 35.0);
UIControl *bgControl = [[UIControl alloc] initWithFrame:bgFrame];
[bgControl addTarget:self
action:@selector(resignKeyboardAction)
forControlEvents:UIControlEventTouchDown];
self.bgView = bgControl;
[bgControl release];
self.bgView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];
self.bgView.hidden = YES;
[self.view addSubview:bgView];

原文地址:https://www.cnblogs.com/wengzilin/p/2598793.html