ios 之UISearchBar隐藏虚拟键盘几种方法
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.- (void)viewDidLoad{ [super viewDidLoad]; // 添加带有处理时间的背景图片 UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; backView.image = [UIImage imageNamed:@"small3.png"]; backView.userInteractionEnabled = YES; UITapGestureRecognizer *singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)]; [backView addGestureRecognizer:singleTouch]; backView.tag = 110; [self.view addSubview:backView]; // 添加uitextfield text = [[UITextField alloc] initWithFrame:CGRectMake(30, 150, 250, 31)]; //[text setBackgroundColor:[UIColor grayColor]]; text.borderStyle = UITextBorderStyleRoundedRect; text.placeholder = @""; [self.view addSubview:text]; // 添加返回按钮 UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(125, 40, 75, 35); [button addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside]; //[button setBackgroundColor:[UIColor grayColor]]; [button setTitle:@"返回" forState:UIControlStateNormal]; [self.view addSubview:button];}-(void)dismissKeyboard:(id)sender{ [text resignFirstResponder];}
第三种方法:在xib文件中,修改xib文件的objects属性,默认是view属性,我们可以修改为UIControl属性,从而是xib文件相应touch down事件。这种方法,缺点就是没有xib就悲剧了。不过按说也应该可以动态设置,目前没有找到方法,那位网友知道的话,不妨告诉我下。
设置参考这里:
把objects设置未control后,可以直接相应touch down事件
综合以上三种方法,编写了一个例子,大家可以下载看看代码