2016-07-25 4 views
1

キーボードが表示されているときはビューを上に移動し、メッセージのようにキーボードが隠れているときは下に移動したい。私はそれを達成することができますが、私は空白があるという予言を隠しています。 enter image description hereUIKeyboard予測のオン/オフでビュー位置を管理する

は、私はそれがメッセージアプリで起こるようにビューの動きを処理したい「UIKeyboardWillShowNotification」と「UIKeyboardWillHideNotification

- (void)viewWillAppear:(BOOL)animated { 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 
} 

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

#pragma mark - keyboard movements 
- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.view.frame; 
     f.origin.y = -keyboardSize.height; 
     self.view.frame = f; 
    }]; 
} 

-(void)keyboardWillHide:(NSNotification *)notification 
{ 
    [UIView animateWithDuration:0.3 animations:^{ 
     CGRect f = self.view.frame; 
     f.origin.y = 0.0f; 
     self.view.frame = f; 
    }]; 
} 

とキーボードの動きを処理しています。

答えて

1
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

ここで線の下の

CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; 
+0

それは罰金仲間を働いていますか? –

0

と交換私の実現である:

- (void)viewDidLoad { 
 
    [super viewDidLoad]; 
 
    self.defaultViewFrame = self.view.frame; 
 
} 
 

 
- (void)viewWillAppear:(BOOL)animated { 
 
    [super viewWillAppear: animated]; 
 
    [[NSNotificationCenter defaultCenter]addObserver:self 
 
              selector:@selector(onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil]; 
 
    [[NSNotificationCenter defaultCenter]addObserver:self 
 
              selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil]; 
 
} 
 

 
- (void)viewWillDisappear:(BOOL)animated { 
 
    [super viewWillDisappear:animated]; 
 
    [[NSNotificationCenter defaultCenter] removeObserver:self]; 
 
} 
 

 
- (void)onKeyboardShow:(NSNotification *)notification {  // method when keyboard appears 
 
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 
 
    [self updateView:YES withKeyboardHeight:keyboardHeight]; 
 
} 
 

 
- (void)onKeyboardHide:(NSNotification *)notification { // method when keyboard hides 
 
    CGFloat keyboardHeight = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 
 
    [self updateView:NO withKeyboardHeight:keyboardHeight]; 
 
} 
 

 
***************************** 
 

 
- (void)updateView:(BOOL)show withKeyboardHeight:(CGFloat)height{ 
 
    [self.view setFrame:self.defaultViewFrame];  
 
    
 
    CGRect newViewRect = CGRectMake(self.view.frame.origin.x, 
 
            self.view.frame.origin.y - height, 
 
            self.view.frame.size.width, 
 
            self.view.frame.size.height); 
 
    
 
    if (show) { 
 
     [UIView animateWithDuration:0.3 animations:^{ 
 
      [self.view setFrame:newViewRect]; 
 
     } completion:^(BOOL finished) {}]; 
 
    } 
 
}

関連する問題