解决键盘遮挡UITextField问题
2013-07-19 23:30
621 查看
// // ViewController.m // UITextFieldScrollDemo // // Created by WeiZhen_Liu on 13-7-19. // Copyright (c) 2013年 WeiZhen_Liu. All rights reserved. // #import "ViewController.h" @interface ViewController () { UITextField *_textField; } @end @implementation ViewController - (void)viewDidLoad { // self.view.frame: 0, 20, 320, 548 [super viewDidLoad]; NSLog(@"%f, %f", self.view.center.x, self.view.center.y); NSLog(@"self.view.frame.size.height: %f", self.view.frame.size.height); // 假设是iPhone5之后,则打印548.000000 去掉了状态栏的 20 _textField = [[UITextField alloc] init]; // 先假设是英文键盘 // [_textField setFrame:CGRectMake(20, self.view.frame.size.height-216-30, 280, 30)]; // 216是键盘高,30是textField高 [_textField setFrame:CGRectMake(20, 352-20-30, 280, 30)]; // 352是键盘originY,20是状态栏高,30是textField高 // 键盘的originY是相对于整个屏幕) _textField.borderStyle = UITextBorderStyleBezel; [self.view addSubview:_textField]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveKeyBoardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil]; } - (void)didReceiveKeyBoardWillShowNotification:(NSNotification *)notification { NSLog(@"************************"); NSDictionary *userInfo = [notification userInfo]; NSLog(@"%@", userInfo); /** { English key board: UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = "0.25"; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; } */ /** { Chinese key board: UIKeyboardAnimationCurveUserInfoKey = 0; UIKeyboardAnimationDurationUserInfoKey = 0; UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 252}}"; UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 390}"; UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 354}"; UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 264}, {320, 216}}"; UIKeyboardFrameChangedByUserInteraction = 0; UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 228}, {320, 252}}"; } */ NSLog(@"--> %@", [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]); NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGFloat keyBoardOriginY = value.CGRectValue.origin.y; NSLog(@"键盘originY:%f", keyBoardOriginY); CGFloat keyBoardHeight = value.CGRectValue.size.height; NSLog(@"键盘height: %f", keyBoardHeight); NSNumber *duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]; // CGFloat offSet = self.view.frame.size.height-keyBoardHeight-CGRectGetMaxY(_textField.frame); CGFloat offSetY = CGRectGetMaxY(_textField.frame)-(self.view.frame.size.height-keyBoardHeight); if (offSetY > 0) { // 如果键盘遮挡了textField [UIView animateWithDuration:duration.doubleValue animations:^{ self.view.frame = CGRectMake(0, 20-offSetY, self.view.frame.size.width, self.view.frame.size.height); // self.view.center = CGPointMake(self.view.center.x, self.view.center.y-offSetY); // 这样设其实是可以的,但是好像是apple的bug,第一次从英文切换到中文时, // 但是这个didReceiveKeyBoardWillShowNotification:方法会调多次,所以不能这样做 }]; } else { // 从中文到英文 [UIView animateWithDuration:0.25f animations:^{ [self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)]; }]; } } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [_textField resignFirstResponder]; [UIView animateWithDuration:0.25f animations:^{ [self.view setFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height)]; }]; } - (void)dealloc { [_textField release]; [super dealloc]; } @end
相关文章推荐
- UIScrollView中的UITextField键盘遮挡问题的最简单解决办法 2012-10-31 15:10 1743人阅读 评论(0) 收藏 举报 两步: 1重载textFieldDidBe
- ios--解决键盘遮挡UITextField控件的问题(方式二)
- UIScrollView中的UITextField键盘遮挡问题的最简单解决办法
- 解决键盘遮挡输入框(UITextField)问题
- UITextField被键盘遮挡问题解决方法
- 解决键盘遮挡输入框(UITextField)问题
- iOS开发笔记之解决键盘遮挡UITextField问题
- ios--解决键盘遮挡UITextField控件的问题(方式一)
- UIScrollView中的UITextField键盘遮挡问题的最简单解决办法
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- 解决键盘遮挡输入框(UITextField)问题
- IOS学习笔记36—解决键盘遮挡输入框(UITextField)问题
- 如何解决UITextField挡住键盘的问题
- 『零行代码』解决键盘遮挡问题(iOS)
- 通用方法解决UITextFiled输入的时候,键盘遮挡问题
- 【iOS 0 行代码系列】之 0 行代码解决键盘遮挡问题
- Android 输入法键盘和activity页面遮挡问题解决
- 从原理上分析解决软键盘的遮挡与隐藏问题, EditText在Toolbar中键盘弹出问题
- IOS开发(21)关于UITextField的键盘遮挡问题
- RN中的键盘遮挡问题解决