本文介绍了纵向的第一个 UIView 和横向的第二个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 2 个视图控制器的应用程序.第一个视图控制器应该是纵向的,这没问题,但是当我加载第二个视图控制器时,我无法使应用程序方向为横向...问题出在 iOS 6 上.

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.

我已经尝试了在 SO 上找到的所有内容.

I have tried everything I found on SO.

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

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

还有:

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

推荐答案

2nd View Controller's viewDidLoad 方法中添加这些代码到 transform view 进入landscape :

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

添加 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 的 方法中将 transform view 转换为 protrait .添加这些:

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

如果没有添加,你的 orientation 方法也应该是这样的:

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 :为 radian

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

这篇关于纵向的第一个 UIView 和横向的第二个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:15