最近做的項目用到好多textField, 涉及到鍵盤回收,鍵盤擋住textField 上移textField等,自己水準不夠,深感textfield好複雜,想好好整理下自己會得用法,繼續學習不會的用法。。
鍵盤回收有好多中方法:UITextFieldDelegate, 在self.view上添加UIControl, TouchesBegan方法,[UIAppllication shareApplication]方法
其中點選textField外任意區域回收鍵盤,這三種方法可以實作:self.view上添加UIControl, TouchesBegan方法,[UIAppllication shareApplication]
方法1 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
1、第一步
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets = NO;
//建立self.view上的各種控件。。。。
//建立完所有控件後,在此添加手勢 保證加在self.view的最外層
//設定點選任何其他位置 鍵盤回收
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBG:)];
[self.view addGestureRecognizer:tapGesture];
}
2、第二步 手勢action 調用UIApplication sharedApplication
- (void)tapBG:(UITapGestureRecognizer *)gesture {
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
即可實作點選textField外任意區域回收鍵盤。。。