我的应用中的方向有问题。假设我有两个视图(使用专用视图控制器):
它被缩小并以肖像显示(如下面的第二张图片)。 当我将设备水平旋转并回到纵向时,一切正常。但在推动视图后它显示不正确(下图)。我该如何解决这个问题?
我使用CustomNavigationController来继承UINavigatorControler并实现三种方法:
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
return [self.topViewController shouldAutorotateToInterfaceOrientation:orientation];
}
在应用程序委托中,我以这种方式初始化控制器:
self.navigationController = [[CustomNavigationController alloc] initWithRootViewController:self.viewController];
[self.window setRootViewController:self.navigationController];
第一个视图控制器以这种方式实现方向功能:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait)
return YES;
return NO;
}
第二个视图控制器以这种方式实现方向功能:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
答案 0 :(得分:1)
嗨声明一个全局变量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);
}
答案 1 :(得分:1)
检查问题How to handle different orientations in iOS 6.在那里查看答案,了解您需要的项目示例。
基本上,您需要在viewcontroller(要旋转的控件)中嵌入自定义导航控制器。在此自定义导航控制器中添加以下方法(如果是横向方向,则可以更改为纵向)。
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
并添加到应旋转的视图控制器:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
确保在项目中启用纵向,横向右和横向左方向。然后,如果要阻止特定窗口的某些方向:
– application:supportedInterfaceOrientationsForWindow:
答案 2 :(得分:0)
要执行此操作您可以使用此功能:
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
您可以在任何地方使用它,但在应用程序委托中(在 didFinishLaunchingWithOptions 中)我必须输入以下代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
它完美无缺!