效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @property (strong, nonatomic) UITextField *txtFMessage; 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)hideKeyboard:(id)sender; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 _txtFMessage = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 260, 30)]; 13 _txtFMessage.center = self.view.center; 14 _txtFMessage.borderStyle = UITextBorderStyleRoundedRect; 15 _txtFMessage.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 16 _txtFMessage.placeholder = @"请输入信息"; 17 [self.view addSubview:_txtFMessage]; 18 19 UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard:)]; 20 [self.view addGestureRecognizer:tapGestureRecognizer]; 21 } 22 23 - (void)didReceiveMemoryWarning { 24 [super didReceiveMemoryWarning]; 25 // Dispose of any resources that can be recreated. 26 } 27 28 /** 29 * 隐藏键盘 30 * 31 * @param sender 事件触发者 32 */ 33 - (void)hideKeyboard:(id)sender { 34 NSLog(@"Tap Gesture Recognizer,去掉某些控件作为第一响应器"); 35 [_txtFMessage resignFirstResponder]; 36 } 37 38 @end
时间: 2024-10-07 10:17:21