您的位置:首页 > 产品设计 > UI/UE

解决键盘遮挡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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: