2016-07-25 9 views
1

Ich möchte die Ansicht nach oben verschieben, wenn die Tastatur angezeigt wird, und nach unten, wenn die Tastatur wie in der Nachricht ausgeblendet ist. Ich bin in der Lage, es zu erreichen, aber ich verberge die Vorhersage, dass es etwas Leerstelle gibt. enter image description hereVerwalten der Ansichtsposition mit der UIKeyboard-Vorhersage ein und aus

Ich bin Umgang mit der Bewegung der Tastatur mit „UIKeyboardWillShowNotification“ und „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; 
    }]; 
} 

Ich mag die Bewegung der Ansicht behandeln, wie es in den Nachrichten App passiert.

Antwort

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

mit unterhalb der Linie ersetzen

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

Es funktioniert gut bro? –

0

meine Erkenntnis ist hier:

- (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) {}]; 
 
    } 
 
}