我的iPhone应用程序是基于导航的,包含许多仅限纵向的视图和一个用于查看图像的仅横向视图。因此,即使设备处于纵向模式,我也希望强制此仅横向视图自动旋转为景观。
所以这就是我在那个视图的控制器中所做的:
// 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模式显示。但是有一个问题。此视图的位置不正确,如this screenshot。
所示我试图在旋转后手动设置框架,但它没有帮助。另外,我没有找到一个完整的指南,如何将一个视图设置为仅景观。 请帮我解决这个问题。
答案 0 :(得分:0)
看看这家伙的post。基本上你需要围绕窗口的中心进行变换。看起来您可能还需要旋转状态栏。
另一种方法是使用UIDevice setDeviceOrientation,但这是一个私有API,因此不是一个很好的解决方案。
答案 1 :(得分:0)
应用转换后尝试使用此方法:
[self.view sizeToFit];