1

私はあらゆる場所を検索しましたが、これの解決策が見つかりませんでした。私はiphoneの新機能です。ナビゲーションの高さを設定した場所、またはビューが問題のように向きで回転していないすべての場所で、ビューは回転していますが、ナビゲーションバーは同じです解決策がある場合は、誰か助けてください。おかげで、オリエンテーションに使用したコードを下に表示しました。タブバーをタップすると、シミュレーターが自動回転し、タブバーも回転しますが、このコードのみを使用しますシミュレーターはタブバーやナビゲーションバーではなく回転しており、英語が下手で申し訳ありません。

CGAffineTransform transform = CGAffineTransformIdentity;

switch ([[UIApplication sharedApplication] statusBarOrientation]) 
{

    case UIInterfaceOrientationPortrait:
        transform = CGAffineTransformMakeRotation(M_PI_2);
        break;

    default:
        break;
}

[[UIApplication sharedApplication]setStatusBarOrientation:UIInterfaceOrientationPortrait];

[UIView animateWithDuration:0.2f animations:^ {

    [self.navigationController.view setTransform:transform];

}];

[self.view setFrame:CGRectMake(0, 0, 320, 480)];
[self.view setNeedsLayout];
4

1 に答える 1

1

このコードは、攻撃を意図したものではなく、非常に興味深いものです。あなたが何をしようとしているのかわかりません。どのような問題を解決しようとしていますか?CGAffineTransformをいじってみると、あまり注意しないと、説明したような奇妙な結果が確実に生成される可能性があります。

アプリが横向きと縦向きを正常にサポートしていることを確認したいだけの場合は、ViewControllerに実装できshouldAutorotateToInterfaceOrientationます。これを行うと、さまざまなコントロールのすべてがそれに応じて向きを変えます。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Support all orientations on iPad
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
        return YES;

    // otherwise, for iPhone, support portrait and landscape left and right

    return ((interfaceOrientation == UIInterfaceOrientationPortrait) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

しかし、私があなたがやろうとしていることを誤解している場合、つまり、横向きと縦向きの両方をサポートするだけではなく、より洗練された何かをしようとしている場合は、私に知らせてください。


このコードを最初にどこで入手したか覚えていないのでお詫びします(ただし、ここではSOで参照されています)が、以下を使用して横向きを強制することができます。

まず、shouldAutoRotateToInterfaceOrientationが次のようになっていることを確認します。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
        (interfaceOrientation == UIInterfaceOrientationLandscapeRight))
        return YES;
    else
        return NO;
}

次に、viewDidLoadに次のコードを追加します。

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation]))
{
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    UIView *view = [window.subviews objectAtIndex:0];
    [view removeFromSuperview];
    [window addSubview:view];
}

何らかの理由で、メインウィンドウからビューを削除してから再度追加すると、shouldAutorotateToInterfaceOrientationにクエリが実行され、方向が正しく設定されます。これがAppleが承認したアプローチではないことを考えると、おそらくそれを使用することを控えるべきですが、それは私にとってはうまくいきます。あなたのマイレージは異なる場合があります。しかし、そのSOの議論は、他の手法にも言及しています。

于 2012-04-18T04:41:15.957 に答える