无法自动更改方向

时间:2013-11-08 13:49:27

标签: ios objective-c

我有一个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时,它无法自动更改方向,我需要手动更改方向,所以我想要一个避免的解决方案此

3 个答案:

答案 0 :(得分: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)];
}

答案 1 :(得分:0)

您尝试使用指定方向(f.e landscape):

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

答案 2 :(得分:0)

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

  • Jatin Chauhan

jatinchauhan.it@gmail.com

相关问题