2012-05-09 18 views
0

私はポートレートとportraitUpsideDownで動作するアプリケーションを持っています。私は、キーボードが表示されたら、ビューをプッシュして、それが消えたら戻します。次のコードは、デバイスがポートレートにとどまっている場合は完全に機能しますが、portraitUpsideDownの場合は、ビューが間違った方向(260ではなく-260)に移動し、キーボードが表示されている間に向きが変化しても処理されません。 keyboardWillHideメソッドは、両方の向きで正常に動作します。どの方向のデバイスがあるかは関係ありませんので、キーボードまたはステータスバーに相対的なビューを移動する方法はありますか?キーボードのビューをポートレート方向に移動する方法

- (void) keyboardWillShow:(NSNotification *) notification 
{ 
    NSLog(@"Keyboard Will Show"); 
    double animationDuration; 
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
     self.view.center = CGPointMake(self.view.center.x, self.view.center.y + -260); 
    }completion:^(BOOL finished){ 
    }]; 
} 

- (void) keyboardWillHide:(NSNotification *) notification 
{ 
    double animationDuration; 
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    NSLog(@"Keyboard Will Hide"); 

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
     [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    }completion:^(BOOL finished){ 
    }]; 
} 

答えて

-1

フレームの高さを設定するか、中心ではなくビューのcontentInsetを設定する必要があります。

+0

問題は、私はフレームの高さを使用する場合、加算または減算するかどうかを知っている必要が同じです... – HackyStack

0

私は(エレガント未満)、このようにそれを解決:

注:あなたがやるべき事を、私は最終的にそれらを行うには、このコードを変更しますが、次のとおりです。私ができるように、キーボードのアニメーションの再生時間を保持するためのプロパティを作成しますキーボードデリゲートメソッドの外でそれを使用し、同様にオフセットのプロパティを作成し、userInfoを使用してキーボードの高さを決定します。

- (void) keyboardWillShow:(NSNotification *) notification 
{ 
    NSLog(@"Keyboard Will Show"); 
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait) 
     offset = -260; 
    else 
     offset = 260; 

    double animationDuration; 
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
     self.view.center = CGPointMake(self.view.center.x, self.view.center.y + offset); 
    }completion:^(BOOL finished){ 
    }]; 
} 

- (void) keyboardWillHide:(NSNotification *) notification 
{ 
    double animationDuration; 
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    NSLog(@"Keyboard Will Hide"); 

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
     self.view.center = CGPointMake(self.view.center.x, self.view.center.y - offset); 
    }completion:^(BOOL finished){ 
    }]; 
} 

- (void) keyboardDidShow 
{ 
    NSLog(@"Keyboard Did Show"); 
    keyboardIsShowing = YES; 
} 

- (void) keyboardDidHide 
{ 
    NSLog(@"Keyboard Did Hide"); 
    keyboardIsShowing = NO; 
} 


-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{  
    if (keyboardIsShowing && UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) 
    { 
     if (toInterfaceOrientation == UIInterfaceOrientationPortrait && offset == 260) 
      offset = -260; 
     else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && offset == -260) 
      offset = 260; 
     else 
      return; 
     [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationCurveEaseIn animations:^{ 
      self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 2* offset); 
     }completion:^(BOOL finished){ 
     }]; 
    } 
} 
+0

うん、それは私が私のアプリの中でやっていたものはかなりあります。作業を確実にするためのテストケースの1つは、キーボードをアクティブにし、デバイスを裏返してから、キーボードを閉じることです。それはあなたがそれをカバーしているように見えます。 –

+0

それはもっと簡単で、少なくとももっとエレガントなやり方があるように思えるでしょう...この作品は...この作品が...すべての方向性をサポートしていれば痛みを想像することはできません... – HackyStack

0

残念ながら、あなた自身でこれを管理する必要があります。現在の方向を調べ、y軸の調整を適切に設定することができます。

int movementDistance = -260; 
if (UIInterfaceOrientationPortraitUpsideDown == [self interfaceOrientation]) movementDistance = -movementDistance;