2013-12-16 113 views
1

我的应用程序中存在方向问题。假设我有两个视图(具有专用视图控制器):如何将视图从纵向旋转到横向模式

  • 第一应(它被正确显示)
  • 第二应(它未正确显示显示在风景显示在画像)

它被缩小并以纵向显示(如下图所示)。 当我旋转设备水平和回到肖像一切都很好。但推后视图显示不正确(下图)。我怎样才能解决这个问题?

enter image description here

我用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; 
} 

回答

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); 


} 
+0

这个工程真棒... –

0

要做到这一点,您可以使用此功能:

[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait]; 

只要你想,你可以利用这一点,但在应用程序代理(在didFinishLaunchingWithOptions)我必须把这个代码:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

这是完美的作品!

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: 
相关问题