iOS UITextField随着键盘的高度变化动态调整位置

在使用中,可以直接把下面的类复制到项目中,在创建文本框的类中添加如下代码:

 self.field=[[UITextField_UIKeyBoard alloc]init];
    CGRect fieldRect=CGRectMake(0, 420, 250, 40);
    if(IS_IPHONE_5)
    {
        fieldRect=CGRectMake(0, 526, 250, 40);
    }
    self.field.rect=fieldRect;
   [self.field textFieldFrame:self.field.rect viewController:self];


提到的类为:

 



#import 

@interface UITextField_UIKeyBoard : NSObject
@property(retain,nonatomic) UITextField * myTextField;
@property(assign,nonatomic) CGRect rect;
-(void)textFieldFrame:(CGRect)_rect viewController:
(UIViewController *)viewController;
@end


#import "UITextField_UIKeyBoard.h"

@implementation UITextField_UIKeyBoard
@synthesize myTextField=_myTextField,rect=_rect;

-(void)dealloc
{
    [self.myTextField release];
    [super dealloc];
}

-(void)textFieldFrame:(CGRect)_rect viewController:
           (UIViewController *)viewController
{
    self.myTextField=[[UITextField alloc] init];//初始化UITextField
    self.myTextField.frame =self.rect;
    self.myTextField.delegate=self;//设置代理
    self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.myTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;//垂直居中
    self.myTextField.placeholder = @"Please entry your content!";//内容为空时默认文字
    self.myTextField.returnKeyType = UIReturnKeyDone;//设置放回按钮的样式
    [viewController.view addSubview:self.myTextField];
    //注册键盘出现与隐藏时候的通知
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboadWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
    //添加手势,点击屏幕其他区域关闭键盘的操作
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    gesture.numberOfTapsRequired = 1;//手势敲击的次数
    [viewController.view addGestureRecognizer:gesture];
}

//键盘出现时候调用的事件
-(void) keyboadWillShow:(NSNotification *)note{
    NSDictionary *info = [note userInfo];
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//键盘的frame
    CGFloat offY = ([UIScreen mainScreen].bounds.size.height-keyboardSize.height)-self.myTextField.frame.size.height;//屏幕总高度-键盘高度-UITextField高度
    [UIView beginAnimations:nil context:NULL];//此处添加动画,使之变化平滑一点
    [UIView setAnimationDuration:0.3];//设置动画时间 秒为单位
    self.myTextField.frame = CGRectMake(self.rect.origin.x, offY, self.rect.size.width, self.rect.size.height);//UITextField位置的y坐标移动到offY
    [UIView commitAnimations];//开始动画效果
    
}
//键盘消失时候调用的事件
-(void)keyboardWillHide:(NSNotification *)note{
    [UIView beginAnimations:nil context:NULL];//此处添加动画,使之变化平滑一点
    [UIView setAnimationDuration:0.3];
    self.myTextField.frame = self.rect;//UITextField位置复原
    
    [UIView commitAnimations];
}
//隐藏键盘方法
-(void)hideKeyboard{
    [self.myTextField resignFirstResponder];
}
//点击return按钮所做的动作:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];//取消第一响应
    return YES;
}

@end


你可能感兴趣的:(iOS UITextField随着键盘的高度变化动态调整位置)