解决键盘遮挡UITextField问题

//
//  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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值