2012-04-25 6 views
0

私のアプリではiPhoneのホームボタンの向きに合わせてUIViewの向きを設定したいと思います。私は次のようにしてそれを行いました:iphone sdkでuiviewの位置を固定する

/* I handled view orientation in shouldAutorotate method*/ 
    -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
     if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationPortrait) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin; 
     else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
      viewForBarButtons.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; 
     return (interfaceOrientation == UIInterfaceOrientationPortrait); 
    } 

ここで、viewForBarButtonsはviewController内のUIViewです。

しかし、私がreturn(interfaceOrientation == UIInterfaceOrientationPortrait)の代わりにYesを返すように設定した場合。それは動作しません。

この問題を解決する方法。誰もがそれを知っているなら、私を助けてください。 同様の機能がLifeCardsアプリに実装されています。 ありがとうございます。

答えて

1

上記の方法は、デバイスの向きが変更されたときにビューを特定の向きに回転するかどうかを決定することです。

return (interfaceOrientation == UIInterfaceOrientationPortrait); 

は、縦向きのみをサポートするように指定しています。

return YES; 

はすべての方向をサポートします。

しかし、ViewControllerをTabBarControllerに入れている場合は、すべてのViewControllerがその特定の方向をサポートしている場合にのみ回転します。

willRotateToInterfaceOrientationにオブジェクトを自動サイズ変更する代わりに、上記のコードを入れてください。

は、しかし、あなたはスペースが対象と左余白との間で好きなだけを置くことができます

UIViewAutoresizingFlexibleLeftMargin

あなただけ指定しているという見解をspecifingことで、一つのことを知っている必要があります。しかし、それはオリエンテーションの変更中に他の側の先の位置を保持するので、オブジェクトの起点を物理的に変更する必要があるかもしれません(viewForBarButtons)

Ok。あなたが言っているのは、homeButtonの横にあるviewForBarButtonsを必要とし、それに応じてサブビューを配置/回転する必要があるということです。

最初にdevie rotaionに登録するか、didRotateInterfaceOrientationを使用して、viewForBarButtonsの回転サブビューを開始します。

#define degreesToRadians(x) (M_PI * x/180.0) 

回転]サブビューは:サブビューオブジェクト

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; 

    if (animated) 
    { 
     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
    } 

    if (orientation == UIInterfaceOrientationPortraitUpsideDown) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(180)); 

    else if (orientation == UIInterfaceOrientationLandscapeRight) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(90)); 

    else if (orientation == UIInterfaceOrientationLandscapeLeft) 
     self.transform = CGAffineTransformRotate(CGAffineTransformIdentity, degreesToRadians(-90)); 
    else 
     self.transform=CGAffineTransformIdentity; 

    if (animated) 
     [UIView commitAnimations]; 
+0

で自己を置き換えるしかし、私は、ビューのサブビューを起源を変更する場合は邪魔取得します。 – Girish

+0

1つの可能性があります。サイズを持つ親ビューを作成し(viewForBarButtonsよりもすべての側に必要なだけのパディング)、viewForBarButtonsをそのビューに追加します。 viewForBarButtonの目的の自動サイズ変更マスクを設定します。 contatinerにスーパービューを追加し、ビュー全体にサイズを変更してください(柔軟な高さと幅を設定するのを忘れないでください)。これはうまくいくはずです。 –

+0

私はそれが "オリエンテーションの変更の間、他の側の前のポジション"になるとコメントしました。追加されたスーパービューに基づくポジションに基づいてケースを検討します。以前の間違いに対する謝罪 –