ランドスケープモードに設定したときに正しく配置されている表示を表示します

StackOverflow https://stackoverflow.com/questions/2086214

質問

私のiPhoneアプリケーションはナビゲーションベースで、多くのポートレートのみのビューと、画像を表示するための1つのランドスケープのみのビューが含まれています。そのため、デバイスがポートレートモードに配置されていても、このランドスケープのみのビューに自動的にLanscapeに回転するように強制したいと思います。

だから、このビューのコントローラーで私がやったことは次のとおりです。

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
}

- (void)viewWillAppear:(BOOL)animated
{
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

 _originalTransform = [[appDelegate navigationController].view transform];
 _originalBounds = [[appDelegate navigationController].view bounds];
 _originalCenter = [[appDelegate navigationController].view center];

 CGAffineTransform landscapeTransform = CGAffineTransformMakeRotation(degreesToRadian(90));
 landscapeTransform = CGAffineTransformTranslate (landscapeTransform, +80.0, +100.0);

 [[appDelegate navigationController].view setTransform:landscapeTransform];

 [appDelegate navigationController].view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
 [appDelegate navigationController].view.bounds  = CGRectMake(0.0, 0.0, 480.0, 320.0);
 [appDelegate navigationController].view.center  = CGPointMake (240.0, 160.0);

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
 }
}

- (void)viewWillDisappear:(BOOL)animated
{ 
 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
 [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 [[appDelegate navigationController].view setTransform:_originalTransform];
 [[appDelegate navigationController].view setBounds:_originalBounds];
 [[appDelegate navigationController].view setCenter:_originalCenter];

 [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait; 
}

今、私はそのビューを常にLanscapeモードで表示しています。しかし、1つの問題があります。このビューは、示されているように誤って配置されます このスクリーンショット.

回転後にフレームを手動で設定しようとしましたが、役に立ちませんでした。また、1つのビューをランドスケープのみに設定する方法を完全なガイドが見つかりませんでした。 この問題について私を助けてください。

役に立ちましたか?

解決

この男をチェックしてください 役職. 。基本的に、窓の中央の周りに変換する必要があります。ステータスバーを回転させる必要があるようです。

別の方法は、uidevice setDeviceorientationを使用することですが、それはプライベートAPIであり、したがって素晴らしい解決策ではありません。

他のヒント

変換を適用した後、この方法を使用してみてください。

[self.view sizeToFit];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top