[英]How to rotate view from portrait to landscape mode
I have a problem with orientations in my app. 我在我的应用程序中遇到方向问题。 Assume that I have two views (with dedicated view controllers): 假设我有两个视图(带有专用的视图控制器):
It is coarctated and displayed in portrait (like in second image below). 它被缩窄并以纵向显示(如下面的第二幅图像所示)。 When I rotate device horizontal and back to portrait everything is OK. 当我将设备水平旋转并回到纵向时,一切正常。 But after pushing view it displays incorrectly (images below). 但是在推送视图后,它显示不正确(下图)。 How can I fix this? 我怎样才能解决这个问题?
I use CustomNavigationController whish inherits from UINavigatorControler and implements three methods: 我使用CustomNavigationController继承自UINavigatorControler并实现了三种方法:
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}
In application delegate I initializing controller in this way: 在应用程序委托中,我以这种方式初始化控制器:
self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
First view controller implements orientation functions in this way: First View控制器通过以下方式实现定位功能:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait)
return YES;
return NO;
}
Second view controller implements orientation functions in this way: 第二个视图控制器以这种方式实现定位功能:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
hi declare a global variable BOOL isLandScape ; 嗨,声明一个全局变量BOOL isLandScape;
initialize it as isLandScape=NO;
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if ((orientation == UIInterfaceOrientationLandscapeRight)||(orientation == UIInterfaceOrientationLandscapeLeft))
{
isLandScape=YES;
return YES;
}
else
{
isLandScape=NO;
return NO;
}
yourObject.frame=CGRectMake(isLandScape?0:0,isLandScape?0:0,isLandScape?1024:768,isLandScape?768:1024);
}
Check the question How to handle different orientations in iOS 6. See the answer there for a project example of exactly what you need. 选中问题“ 如何在iOS 6中处理不同的方向”。有关确实需要的项目示例,请参见此处的答案。
Basically you need to embed a custom navigation controller in your viewcontroller (the one you want to rotate). 基本上,您需要在视图控制器(要旋转的视图控制器)中嵌入自定义导航控制器。 Add the following method in this custom navigation controller (this if for landscape orientation but you can change to portrait too). 在此自定义导航控制器中添加以下方法(如果用于横向,则可以更改为纵向)。
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
and add to your view controller that should rotate: 并添加到应该旋转的视图控制器中:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Be sure Portrait, Landscape Right and Landscape Left orientations are enabled in your project. 确保在项目中启用了“纵向”,“向右横向”和“向左横向”方向。 Then, if you want to block some orientations for a particular window: 然后,如果要阻止特定窗口的某些方向:
– application:supportedInterfaceOrientationsForWindow:
To do this You can use this function: 为此,您可以使用以下功能:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
You can use this wherever you want, but in application delegate (in didFinishLaunchingWithOptions ) i must put this code: 您可以在任何地方使用它,但是在应用程序委托中(在didFinishLaunchingWithOptions中 ),我必须放置以下代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
It's works perfectly! 完美的作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.