当前位置: 代码迷 >> 综合 >> iOS 键盘弹出与回收、界面上移和下移
  详细解决方案

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

热度:21   发布时间:2024-01-20 18:34:42.0

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];//键盘的弹出

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];//键盘的消失


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

- (void)keyboardWasShown:(NSNotification*)aNotification

{

    //获得键盘的大小

    NSDictionary* info = [aNotification userInfo];

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;

    

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.25];

    [UIView setAnimationCurve:7];

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

    [UIView commitAnimations];

}


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

- (void)keyboardWillBeHidden:(NSNotification*)aNotification

{

    [UIView beginAnimations:nil context:nil];

    [UIView setAnimationDuration:0.25];

    [UIView setAnimationCurve:7];

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

    [UIView commitAnimations];

}


-(void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


  相关解决方案