iOS之隐藏键盘的方式

 1 一、//触摸空白处隐藏键盘
 2 
 3 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 4 
 5     [_feedBackTextView resignFirstResponder];
 6 
 7     [_telTextField resignFirstResponder];
 8 
 9 }
10 
11  
12 
13 二、添加点按手势 隐藏键盘
14 
15 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]  initWithTarget:self action:@selector(dismissKeyboard)];
16 
17     [self.view addGestureRecognizer:tap];
18 
19  
20 
21 - (void)dismissKeyboard {
22 
23     [self.userText resignFirstResponder];
24 
25     [self.passWordText resignFirstResponder];
26 
27 }
28 
29  
30 
31 三、//在键盘上方添加一个完成按钮来收起键盘
32 
33     UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
34 
35     [topView setBarStyle:UIBarStyleDefault];
36 
37     //定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
38 
39     UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
40 
41     UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
42 
43     //定义完成按钮
44 
45     UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)];
46 
47     //在toolBar上加上这些按钮
48 
49     NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil]; [topView setItems:buttonsArray];
50 
51     [_feedBackTextView setInputAccessoryView:topView];
52 
53     [_telTextField setInputAccessoryView:topView];
54 
55  
56 
57 //隐藏键盘
58 
59 - (void)resignKeyboard {
60 
61     [_feedBackTextView resignFirstResponder];
62 
63     [_telTextField resignFirstResponder];
64 
65 }
66 
67  

 

posted @ 2016-01-28 09:48  Jaycee麦子  阅读(374)  评论(0编辑  收藏  举报