First UIView in Portrait orientation and second in Landscape

喜你入骨 提交于 2019-12-11 07:04:18

问题


I have an app which has 2 view controllers. The first viewcontroller should be in portrait and it's ok, but when I'm loading the second view controller, I could not make the app orientation to be landscape... The problem is with iOS 6.

I have tried everything I found on SO.

Using [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; on viewWillAppear and viewDidLoad,

and also:

- (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;
}

回答1:


Add these code in 2nd View Controller's viewDidLoad method to transform view into 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)]; 

add rotateController method :

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

Now in viewWillDisappear's method to transform view into protrait . Add these:

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

Also your orientation methods should be like these if not added add these :

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

- (NSUInteger)supportedInterfaceOrientations
{
  return UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
  return UIInterfaceOrientationLandscapeLeft;
}

EDIT : Add these macro for radian

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



回答2:


you may add this code on this view controller where you want to fix your orientation Code as Below

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


}

and second view controller you may want to rotate all orientation then not necessary to adding any code ios6 will manage the orientation automatically.




回答3:


You need to select the landscape mode in application summary-> deployment info.

Also if you are using xib file to design the UI, you need to set the orientation of second view controller to landscape




回答4:


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

-(BOOL)shouldAutorotate
{
     return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

You were not returning the NO in the method shouldAutorotate , replace you existing code with this . Hope it will help you.




回答5:


Prior to iOS 6 i.e in iOS 5 and earlier, an app and a view controller’s rotation is controlled by the individual viewcontrollers while in iOS 6 and later, the view controllers responsible for rotation are the container Viewcontrollers such as UINavigationController & UITabBarController . What are you using as the rootviewcontroller in your project??

Autorotation is clearly explained here in this post- Autorotation in iOS



来源:https://stackoverflow.com/questions/16392021/first-uiview-in-portrait-orientation-and-second-in-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!