解決虛擬鍵盤擋住控件

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //監聽鍵盤高度的變換
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    // 鍵盤高度變化通知,ios5.0新增的
#ifdef __IPHONE_5_0
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 5.0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
#endif
}
#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification {
    
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    [self autoMovekeyBoard:keyboardRect.size.height];
}
//鍵盤彈起界面上拉
-(void) autoMovekeyBoard: (float) h{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    //headView動畫上移
    //你的界面上拉上去代碼
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [self autoMovekeyBoard:0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    //headView動畫上移
    
    //界面恢復原有位置 某個view的frame恢復原樣
    [UIView commitAnimations];
    [self.view endEditing:NO];
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章