【问题标题】:Turn screen in landscape mode when iPhone is in landscape当 iPhone 处于横向模式时,以横向模式转动屏幕
【发布时间】:2014-07-01 19:26:45
【问题描述】:

我想制作一个 iPhone 应用程序,它在 iPhone 处于纵向模式时显示一个视图,而在 iPhone 处于横向模式时显示另一个。 我知道有很多关于这个的帖子,但我不明白答案。

为了理解,我第一次使用选项卡式应用程序进行测试,因为我已经有两个视图。当我点击第二个屏幕时,我希望我的 iPhone 处于横向模式。 (并且在第一个纵向模式中)。

在 Apple 网站和 stackoverflow 上,我看到了以下代码:

- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
   if ((orientation == UIInterfaceOrientationPortrait) ||
   (orientation == UIInterfaceOrientationLandscapeLeft))
  return YES;

   return NO;
}

或类似的代码。

在我的主故事板中,我将第二个视图放在横向,带有界面。

但是当我运行我的应用程序并点击第二个屏幕时,iPhone 保持纵向模式..

我尝试用单视图应用程序做同样的事情,并用 .xib 文件创建了新文件 (landscapeViewController),但我无法得到一个完美的结果!

【问题讨论】:

  • 您可以尝试以编程方式切换模式:stackoverflow.com/questions/10670834/…
  • 这个方法[[UIDevice currentDevice] setOrientation: UIDeviceOrientationLandscapeLeft];在iOS7中不起作用(只读)这个代码也是一样的:if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) { UIWindow *window = [[UIApplication sharedApplication] keyWindow]; UIView *view = [window.subviews objectAtIndex:0]; [view removeFromSuperview]; [window addSubview:view]; }(非常奇怪的代码......)

标签: iphone objective-c landscape


【解决方案1】:

首先,在情节提要中,创建从纵向视图控制器到横向视图控制器的转场,反之亦然。然后,在您的纵向视图控制器中,执行以下操作:

- (void)viewWillLayoutSubviews
{
    if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        [self performSegueWithIdentifier:@"SegueToLandscapeViewController" sender:self];
    }
}

在您的横向视图控制器中,执行以下操作:

- (void)viewWillLayoutSubviews
{
    if (!UIInterfaceOrientationIsLandscape(self.interfaceOrientation)) {
        [self performSegueWithIdentifier:@"SegueToPortraitViewController" sender:self];
    }
}

【讨论】:

  • 如果我想在视图中强制使用横向模式,为什么必须创建 segues?
  • Segues 用于在用户旋转他或她的 iOS 设备时从纵向视图控制器切换到横向视图控制器并再次切换回来。如果您希望其中一个选项卡显示为纵向,而另一个选项卡显示为横向,无论设备方向如何,我不知道这是否可能。这真的是你想要的吗?还是您只是使用选项卡作为在视图控制器之间切换的便捷方式?
【解决方案2】:

我用下面的代码成功了:

-(void)orientationDidChanged: (NSNotification *)notification {

UIDeviceOrientation devOrientation = [UIDevice currentDevice].orientation;

if (UIDeviceOrientationIsLandscape(devOrientation)) {

    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    landscapeViewController *landscape = [main instantiateViewControllerWithIdentifier:@"landscape"];

    [self presentViewController:landscape animated:YES completion:nil];

}

else if (UIDeviceOrientationIsPortrait(devOrientation)) {

    [self dismissViewControllerAnimated:YES completion:nil];   
}
}

但是我不能改变视图控制器之间的转换,而我可以在简单视图之间改变。

也许是因为我有两个视图控制器链接到同一个视图控制器.h...?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2011-01-04
    • 1970-01-01
    • 2013-02-10
    • 2011-06-28
    相关资源
    最近更新 更多