1. 程式人生 > >iOS 鍵盤彈出與回收、介面上移和下移

iOS 鍵盤彈出與回收、介面上移和下移

//新增通知,來控制鍵盤和輸入框的位置

    [[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 = [[info objectForKey: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:nil

context:nil];

    [UIViewsetAnimationDuration:0.25];

    [UIViewsetAnimationCurve:7];

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

    [UIViewcommitAnimations];

}

-(void)dealloc

{

    [[NSNotificationCenterdefaultCenter] removeObserver:self];

}