IOS4与IOS5输入框随键盘移动问题

来源:互联网 发布:黑暗之魂3画面设置优化 编辑:程序博客网 时间:2024/06/04 08:18

- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view.

    

    _textfield = [[UITextFieldalloc]initWithFrame:CGRectMake(10,460-100-26,300-34,26)];

    _textfield.borderStyle =UITextBorderStyleRoundedRect;

    _textfield.tag =444;

    _textfield.delegate =self;

    [_textfieldaddTarget:selfaction:@selector(keyboardWillShow:)forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:_textfield];

    

}

-(void)keyboardWillShow:(UITextField*)textfield

{

    //iOS5中,新增有notificationUIKeyboardWillChangeFrameNotification)可以用来监测键盘frame的变化。在iOS4中,可以通过UIKeyboardWillShowNotification以及UIKeyboardWillHideNotification来监测键盘的显示与隐藏事件

    /*****************/

    //  notification由系统发送,所以无需自己调用发送通知

    /*****************/

    

    /*  通知描述:

        UIKeyboardWillShowNotification键盘即将显示

        UIKeyboardDidShowNotification键盘已经显示

        UIKeyboardWillHideNotification键盘即将隐藏

        UIKeyboardDidHideNotification键盘已经隐藏

        UIKeyboardWillChangeFrameNotification键盘frame即将改变

        UIKeyboardDidChangeFrameNotification键盘frame已经改变

    */

    

    //注册监测事件

    if (IOS_VERSION <5.0) {

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillShowNotificationobject:nil];

        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillHideNotificationobject:nil];

    }else{


        [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWillChangeFrame:)name:UIKeyboardWillChangeFrameNotificationobject:nil];

    }

    

}


- (void)keyboardWillChangeFrame:(NSNotification *)notification

{

    //获取键盘信息

    NSDictionary *info=[notification userInfo];

    NSLog(@"info = %@",info);

    

    //获取键盘的size

    CGSize _keyBord = [[infoobjectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

    

    //获取键盘出现的动画时间

    NSValue *animationDurationValue = [infoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];

    NSTimeInterval animationDuration;

    [animationDurationValue getValue:&animationDuration];

    

    //获取键盘高度

    CGFloat height = _keyBord.height;

    NSLog(@"hhh = %f",height);


    NSTimeInterval animation = animationDuration;

    //视图移动的动画开始

    [UIViewbeginAnimations:@"animal"context:nil];

    [UIViewsetAnimationDuration:animation];

    [_textfield setFrame:CGRectMake(10,460-height-26, 300-24,26)];

    [UIViewcommitAnimations];

}


- (BOOL)textFieldShouldReturn:(UITextField *)textField

{

    //移除监测事件

    [[NSNotificationCenterdefaultCenter] removeObserver:selfname:UIKeyboardWillChangeFrameNotificationobject:nil];

    

    NSTimeInterval animationDuration = 0.30f;

    [UIViewbeginAnimations:@"ResizeForKeyboard"context:nil];

    [UIView setAnimationDuration:animationDuration];


    [_textfield setFrame:CGRectMake(10,460-100-26,300-34, 26)];

 

    [UIViewcommitAnimations];

    [textField resignFirstResponder];

   // textfield.text = nil;

    

    return YES;

}

原创粉丝点击