1

我有一個應用程序,它有2個視圖控制器。第一個viewcontroller應該是肖像,這是可以的,但是當我加載第二個視圖控制器時,我無法使應用程序方向爲風景...問題與iOS 6.第一個UIView縱向和第二個橫向

我已經嘗試了一切我在SO上找到了。

使用上viewWillAppearviewDidLoad[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

也:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
    if (interfaceOrientation == UIInterfaceOrientationPortrait) { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
    } else { 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
    } 
    } 

-(BOOL)shouldAutorotate 
    { 
return YES; 
    } 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
return UIInterfaceOrientationLandscapeRight; 
} 

-(NSInteger)supportedInterfaceOrientations{ 

return UIInterfaceOrientationMaskLandscapeRight; 
} 
+0

你試試我的答案在[這裏] [1]:http://stackoverflow.com/questions/15110838/xcode-how-但是仍然允許一個視圖/ 15112927#15112927 – Ushan87 2013-05-06 05:03:32

回答

1

2nd View Controller'sviewDidLoad方法transformview添加這些代碼到landscape

[self rotateController:self degrees:-90]; 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:NO]; 
//set it according to 3.5 and 4.0 inch screen in landscape mode 
[self.view setBounds:CGRectMake(0, 0, 480, 320)]; 

添加rotateController方法:

-(void) rotateController:(UIViewController *)controller degrees:(NSInteger)aDgrees 
{ 
    UIScreen *screen = [UIScreen mainScreen]; 
    if(aDgrees>0) 
    controller.view.bounds = CGRectMake(0, 0, screen.bounds.size.height, screen.bounds.size.width); 
    else 
    { 
    controller.view.bounds = CGRectMake(0, 0, screen.bounds.size.width, screen.bounds.size.height); 
    } 
    controller.view.transform = CGAffineTransformConcat(controller.view.transform, CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(aDgrees))); 
} 

現將viewWillDisappear's方法改爲transformview改爲protrait。添加這些:

[self rotateController:self degrees:90]; 
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 
//set it according to 3.5 and 4.0 inch screen in protrait mode 
[self.view setBounds:CGRectMake(0, 0, 320, 480)]; 

而且你orientation方法應該是這樣的,如果沒有添加添加這些:

- (BOOL)shouldAutorotate 
{  
    //make view landscape on start 
    return NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeLeft; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeLeft; 
} 

編輯:添加這些macroradian

#define DEGREES_TO_RADIANS(angle) ((angle)/180.0 * M_PI) 
0

你可能要解決您的方向 代碼,下面這個觀點控制器上添加以下代碼

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
     { 
      if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) 
     { 
        return UIInterfaceOrientationMaskPortrait; 

      } 

    } 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 

    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) 
    { 

     return (toInterfaceOrientation==UIInterfaceOrientationPortrait); 
    } 


} 

和第二個視圖控制器你可能想旋轉所有的方向,然後沒有必要添加任何代碼ios6將自動管理方向。

+0

也試過這個轉到您的項目設置的摘要部分並禁用支持的界面方向。 – Jitendra 2013-05-06 04:51:20

0

您需要在應用程序摘要 - >部署信息中選擇橫向模式。

此外,如果你正在使用XIB文件來設計的UI,你需要第二個視圖控制器方向設置爲橫向

0
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

-(BOOL)shouldAutorotate 
{ 
    return NO; 
} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return UIInterfaceOrientationLandscapeRight; 
} 

-(NSInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskLandscapeRight; 
} 

你沒有回覆我在方法shouldAutorotate中,用這個替換你現有的代碼。 希望它能幫助你。

0

在iOS 6之前,即在iOS 5和更早版本中,應用程序和視圖控制器的旋轉由各個視圖控制器控制,而在iOS 6和更高版本中,負責旋轉的視圖控制器是容器Viewcontrollers,例如UINavigationController & UITabBarController 。你在項目中使用什麼作爲rootviewcontroller?

自轉顯然這裏解釋在這個後 Autorotation in iOS