iOS 键盘弹出与回收、界面上移和下移

来源:互联网 发布:瓶中小人真理对话知乎 编辑:程序博客网 时间:2024/05/20 11:48

//添加通知,来控制键盘和输入框的位置

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWasShown:)name:UIKeyboardWillShowNotificationobject:nil];//键盘的弹出

    [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillBeHidden:)name:UIKeyboardWillHideNotificationobject:nil];//键盘的消失


#pragma mark ----- 键盘显示的时候的处理

- (void)keyboardWasShown:(NSNotification*)aNotification

{

    //获得键盘的大小

   NSDictionary* info = [aNotification userInfo];

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

    

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:0.25];

    [UIViewsetAnimationCurve:7];

   self.view.frame =CGRectMake(0,-kbSize.height/6 ,VCWidth, VCHeight);

    [UIViewcommitAnimations];

}


#pragma mark -----    键盘消失的时候的处理

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    [UIViewbeginAnimations:nilcontext:nil];

    [UIViewsetAnimationDuration:0.25];

    [UIViewsetAnimationCurve:7];

   self.view.frame =CGRectMake(0,0, VCWidth,VCHeight);

    [UIViewcommitAnimations];

}


-(void)dealloc

{

    [[NSNotificationCenterdefaultCenter] removeObserver:self];

}


0 0
原创粉丝点击