iOS第三方键盘高度获取方法
2015-05-13 16:46
627 查看
ios8允许第三方键盘使用后,键盘的高度经常不准确,怎么破,不废话,上代码。
#import "ViewController.h" @interface ViewController () @property (nonatomic,strong) UITextField *textField; @end @implementation ViewController #pragma mark - lifeCycle - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.view addSubview:self.textField]; [self adjustFrame]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextFieldFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)adjustFrame { _textField.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - 40, CGRectGetWidth(self.view.bounds), 40); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - privateMethod - (void)changeTextFieldFrame:(NSNotification *)noti { NSDictionary *userInfo = noti.userInfo; [UIView animateWithDuration:[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{ NSDictionary* d = [noti userInfo]; #pragma mark - 获取键盘frame CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect frame = self.textField.frame; frame.origin.y = CGRectGetMinY(r) - CGRectGetHeight(frame); self.textField.frame = frame; }]; } #pragma mark - 触屏事件 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.textField resignFirstResponder]; } #pragma mark - get - (UITextField *)textField { if (_textField == nil) { _textField = [[UITextField alloc] init]; _textField.backgroundColor = [UIColor lightGrayColor]; } return _textField; } @end
该段代码的核心: CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
用这段话可以比较准确的监听到键盘的高度。
相关文章推荐
- iOS键盘中英文切换键盘高度获取通知方法
- iOS动态获取键盘高度方法
- iOS第三方键盘高度获取不准确问题
- iOS获取键盘高度的方法
- iOS键盘中英文切换键盘高度获取通知方法
- iOS获取键盘的高度
- iOS获取键盘的高度
- 键盘弹起与隐藏时高度的获取及动画 适配所有iOS系统
- iOS获取键盘的高度
- iOS获取系统键盘的高度
- ios5 中文键盘高度变高覆盖现有ui问题的解决方案(获取键盘高度的方法)
- iOS 禁用第三方输入键盘方法
- IOS下获取当前用户设备中的键盘的高度
- 获取第三方键盘高度(包括自带键盘高度)
- iOS 键盘弹出时获取键盘的高度
- IOS 开发如何获取键盘高度
- iOS获取键盘的高度(在键盘出现,改变和消失时通知改变)
- iOS笔记监听键盘状态 获取键盘高度
- iOS获取键盘高度
- iOS键盘高度的获取