0

ルート コントローラーが UINavigationController であるアプリがあります。1 つのボタンを押すと、別のビュー コントローラーがプッシュされます。もう 1 つプッシュすると、Overlay を使用して AVCaptureSession が開始されます。そのビューが欲しいのですが、そのビューは縦向きではなく横向きのみです。さまざまなチュートリアルを見てきましたが、何も機能していません。

私が得た最も近いものは次のとおりです。

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

ビューでDidAppear。ただし、いくつかの問題があります。1 つ目は、すぐにポートレートに戻ることです。2 つ目は、他のビューはすべて縦向きと横向きにすることができますが、私はそれを望んでいません。助けが必要です、お願いします。

4

2 に答える 2

0

はい、appDelegate に次のメソッドを配置することで、特定の viewController をランドスケープとして作成できます。

-(NSUInteger)application:(UIApplication *)application    supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
    return UIInterfaceOrientationMaskPortrait;
else
    return UIInterfaceOrientationMaskAll;
}

次に、appDelegate で bool プロパティを 1 つ取得します

@property (nonatomic) BOOL restrictRotation; 

次に、向きを変更したいView ControllerでappDelegate共有インスタンスを作成し、以下の方法のように制限を変更できるようにします

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
} 

ビューコントローラーからこのメソッドを呼び出して、必要に応じて向きを変更してください。

[self restrictRotation:NO];

    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

最も重要なことは、ビューが消えるときに向きを縦向きに変更することです。そうしないと、他のすべてのviewControllerも横向きモードになります。

-(void)viewWillDisappear:(BOOL)animated
{
    NSNumber *value = [NSNumber     numberWithInt:UIDeviceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    [self restrictRotation:YES];
} 

これがお役に立てば幸いです。

于 2016-07-30T07:23:34.640 に答える