2017-12-12 5 views
0

私はキーボードがアニメーション化されているときに私のビューを移動できるように、iOSキーボードの高さを取得しようとしています。しかし、私は値258を返し続けます(これは正しくありません。なぜなら、私のビューが高すぎるからです)なぜこれが起こっていますか?以下のコード:Obj-c - 間違ったキーボードの高さが返されますか?

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]; 

    } 

答えて

0

キーボードの高さを得るために、あなたはUITabBarControllerを使用している場合、あなたは計算する必要が代わりにUIKeyboardWillChangeFrameNotification.

-(void)viewDidLoad { 

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

} 

UIKeyboardWillShowNotificationUIKeyboardDidShowNotificationを使用する必要がありますタブバーの高さなしのフレーム。以下のコードでタブバーの高さを取得できます。

self.tabBarController.tabBar.frame.height 
+0

まだ258の高さを返します - いいえサイコロを。 – Brittany

+0

この値はどこで記録しましたか? 'UITabBarController'を使用していますか? – trungduc

+0

はい、ビューはUITabBarControllerに埋め込まれています。私はanimateTextViewの内部とkeyboardWillChangeの内部に値を記録します。 – Brittany

0

キーボードの高さを観察するには、UIKeyboardWillShowNotificationを登録する必要があります。

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

2)あなたもこのコードを試すことができます。

(void)keyboardWillChange:(NSNotification *)notification 
{ 
    NSDictionary* keyboardInfo = [notification userInfo]; 
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue]; 
} 
関連する問題