2017-12-12 3 views
0

Ich versuche, die Höhe der iOS-Tastatur zu erreichen, so dass ich meine Ansicht verschieben kann, wenn die Tastatur animiert ist. Allerdings bekomme ich immer den Wert 258 zurück (was ist nicht korrekt, da es meine Sicht zu hoch treibt?) Warum passiert das? Code unten:Obj-c - Falsche Tastaturhöhe wird zurückgegeben?

ViewController.m

-(void)viewDidLoad { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; 

} 
    - (void)keyboardWillChange:(NSNotification *)notification { 

     NSDictionary* keyboardInfo = [notification userInfo]; 

     NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey]; 

     CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; 
     self.keyboardHeight = keyboardFrameBeginRect.size.height; 


    } 

    - (void) animateTextView:(BOOL) up 
    { 

     const int movementDistance = self.keyboardHeight; 

     const float movementDuration = 0.3f; 
     int movement= movement = (up ? -movementDistance : movementDistance); 


     [UIView beginAnimations: @"anim" context: nil]; 
     [UIView setAnimationBeginsFromCurrentState: YES]; 
     [UIView setAnimationDuration: movementDuration]; 

     self.upView.frame = CGRectOffset(self.upView.frame, 0, movement); 
     [UIView setAnimationDidStopSelector:@selector(afterAnimationStops)]; 
     [UIView commitAnimations]; 

    } 

Antwort

0

Zur Höhe der Tastatur zu erhalten, sollten Sie UIKeyboardWillShowNotification oder UIKeyboardDidShowNotification statt UIKeyboardWillChangeFrameNotification.

-(void)viewDidLoad { 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil]; 

} 

verwenden Wenn Sie UITabBarController verwenden, müssen Sie berechnen Rahmen ohne Tab Bar Höhe. Sie können die Höhe der Tab-Leiste mit dem folgenden Code ermitteln.

self.tabBarController.tabBar.frame.height 
+0

Noch gibt Höhe von 258 - ohne Würfel. – Brittany

+0

Wo haben Sie diesen Wert protokolliert? Verwenden Sie 'UITabBarController'? – trungduc

+0

Ja, die Ansicht ist in einen UITabBarController eingebettet. Ich protokolliere den Wert in animateTextView und in keyboardWillChange. – Brittany

0

Sie sollten UIKeyboardWillShowNotification registrieren, um die Tastaturhöhe zu beobachten.

1) [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil]; 

2) Sie können diesen Code versuchen:

(void)keyboardWillChange:(NSNotification *)notification 
{ 
    NSDictionary* keyboardInfo = [notification userInfo]; 
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; 
} 
Verwandte Themen