0

我有一个 TabBarController,里面有两个 UIViewControllers,为了适应横向,我在第一个 ViewController 中添加了以下代码:

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

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
      //handle with portait
}else{
      //handle with landscape
   }
}

实际上它可以工作,但是我遇到了一个问题:当我在第二个 ViewController 中更改方向并返回第一个 ViewController 时,它不能自动更改方向,我需要手动更改方向,所以我想要一个解决方案来避免这种情况

4

3 回答 3

1
[[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationLandscapeLeft];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:true];

这会做。并且不要忘记在此方法调用之后更改视图的框架,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationLandscapeLeft];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:true];

    // Screen is turned now, change all views their frames
    [myButton setFrame:CGRectMake(480, 20, 0, 0)];
    [myLabel setFrame:CGRectMake(480, 50, 0, 30)];
}
于 2013-11-08T15:02:46.637 回答
0

您会尝试用于指定您想要的方向(fe 横向):

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}
于 2013-11-08T14:19:13.783 回答
0

在代码之前,您必须在项目 > 摘要中检查您的设备 - 您必须单击纵向、横向。(支持的界面方向)根据您的方向要求。

  • 贾廷·乔汉

jatinchauhan.it@gmail.com

于 2013-11-08T14:43:41.080 回答