UITextField被弹出键盘遮挡

之前在项目中一直用第三方的库解决的UITextField与键盘遮挡的问题,虽然好用但还是有些不趁手的地方,所以希望了解具体操作的原理以方便日后自己改造。

下面为转载的一片原理分析及源码,挺详细的,转载一下以备日后实践之用。原文地址:点击打开链接


本人定义了一个基本的ViewController——BaseViewController

.h文件内容如下:

#import <UIKit/UIKit.h>


@interface BaseViewController :UIViewController<UITextFieldDelegate>{

   UITextField *_checkText; //用来标识哪一个UITextField被点击

 

@property(nonatomic) UITextField*checkText;

 

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval;

 

@end

 

.m文件内容如下:

 

#import "BaseViewController.h"

 

@implementation BaseViewController

 

 

-(void)viewDidLoad{

   [super viewDidLoad];

 

   //注册通知

   [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotificationobject:nil];

   [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotificationobject:nil];

   

   // 键盘高度变化通知,ios5.0新增的

#ifdef __IPHONE_5_0

   float version = [[[UIDevice currentDevice] systemVersion] floatValue];

   if (version >= 5.0) {

       [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)name:UIKeyboardWillChangeFrameNotification object:nil];

    }

#endif

   

}

 

#pragma mark 开始编辑UITextField,本人试过这个方法在keyboardWillShow之前被调用

-(void)textFieldDidBeginEditing:(UITextField*)textField{

 

   _checkText = textField;//设置被点击的对象

 

}

 

#pragma mark -键盘弹出时调用的方法

#pragma mark Responding to keyboard events

- (void)keyboardWillShow:(NSNotification*)notification {

   if (nil == _checkText) {

       return;

    }

   /*

    Reduce the size of the text view so that it's not obscured by thekeyboard.

    Animate the resize so that it's in sync with the appearance of thekeyboard.

    */

   

   NSDictionary *userInfo = [notification userInfo];

   

   // Get the origin of the keyboard when it's displayed.

   NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

   

   // Get the top of the keyboard as the y coordinate of its origin inself's view's coordinate system. The bottom of the text view's frame shouldalign with the top of the keyboard's final position.

   CGRect keyboardRect = [aValue CGRectValue];

   

   // Get the duration of the animation.

   NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

   NSTimeInterval animationDuration;

   [animationDurationValue getValue:&animationDuration];

   

   CGRect textFrame = _checkText.frame;//当前UITextField的位置

   float textY = textFrame.origin.y + textFrame.size.height;//得到UITextField下边框距离顶部的高度

   float bottomY = self.view.frame.size.height - textY;//得到下边框到底部的距离

   if(bottomY >=keyboardRect.size.height ){//键盘默认高度,如果大于此高度,则直接返回

         return;

    }

   float moveY = keyboardRect.size.height - bottomY;

    

   // Animate the resize of the text view's frame in sync with the keyboard'sappearance.

   [self moveInputBarWithKeyboardHeight:moveYwithDuration:animationDuration];

 

}

 

//键盘被隐藏的时候调用的方法

(void)keyboardWillHide:(NSNotification*)notification {

   

   NSDictionary* userInfo = [notification userInfo];

   

   /*

    Restore the size of the text view (fill self's view).

    Animate the resize so that it's in sync with the disappearance of thekeyboard.

    */

   NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

   [animationDurationValue getValue:&animationDuration];

   

   [self moveInputBarWithKeyboardHeight:0.0withDuration:animationDuration];

}

 

 

 

#pragma mark 移动view

-(void)moveInputBarWithKeyboardHeight:(float)_CGRectHeightwithDuration:(NSTimeInterval)_NSTimeInterval{

  

   CGRect rect = self.view.frame;

   

   [UIView beginAnimations:nil context:NULL];

   

   [UIView setAnimationDuration:_NSTimeInterval];

   

   rect.origin.y = -_CGRectHeight;//view往上移动

 

   self.view.frame = rect;

   

   [UIView commitAnimations];

 

}

 

 

-(void)dealloc{

   [[NSNotificationCenter defaultCenter] removeObserver:self];//在视图控制器消除时,移除键盘事件的通知

    

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值