繁体   English   中英

在iOS中使用单独的视图控制器支持两种方向

[英]Supporting two orientations with separate view controllers in iOS

我现在真是一团糟。 我使用了执行此操作的苹果示例代码:

  1. 创建一个纵向视图控制器和一个横向视图控制器
  2. Potrait事件控制器然后注册设备方向更改通知
  3. 旋转设备时,它会提供一个用于风景视图的模态视图控制器,或者如果将其旋转回纵向,则可以关闭风景视图。
    一切正常,除了一个小问题....

现在我的问题。 我使用它从表视图启动可旋转视图控制器。 它可以旋转并且工作正常。 但是,如果我最初以横向模式启动它,它将仍然以纵向模式启动。 如果要景观,则必须再次将其旋转为景观。为此我尽了最大的努力来修复,但失败了。 您可以从Apple Developer Site Here下载并运行示例代码。 任何人都可以修复此代码,以便在横向模式下启动时可以呈现横向视图的模式视图吗? 否则,我将不得不重写所有内容以使用单个视图控制器。 这些是苹果代码的相关部分:

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithRed:197.0/255.0 green:204.0/255.0 blue:211.0/255.0 alpha:1.0];

LandscapeViewController *viewController = [[LandscapeViewController alloc]
                                                initWithNibName:@"LandscapeView" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                name:UIDeviceOrientationDidChangeNotification object:nil];

}

- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
    [self presentModalViewController:self.landscapeViewController animated:YES];
    isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
    [self dismissModalViewControllerAnimated:YES];
    isShowingLandscapeView = NO;
}    
}

// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return (interfaceOrientation == UIInterfaceOrientationPortrait); // support only portrait}

我知道这可能与您不再相关,但是我遇到了同样的问题,这是我的解决方案。

通过设置代码的方式(包括Apple设置代码的方式)

- (void)updateLandscapeView

仅在发出Notification通知Vi​​ewController方向更改后才调用:这里的问题是,这是负责检查其自身方向的方法。 (即,启动应用程序时不会调用此方法,因此它不会检查设备是否处于其他方向)

解决方案非常简单:在启动时(即在viewDidLoad中)强制校准该方法。

[self  updateLandscapeView]

这将强制调用该方法并检查接口方向,在第一次之后,一旦收到关于方向更改的通知,该方法将再次被调用。

希望这可以帮助某人

除非您仅在设置中指定横向,否则设备似乎采用纵向模式。 唯一的选择是在loadview方法的纵向视图中检测方向并在启动期间交换视图。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
           if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown ) {
        //Load vertical interface
           }
    else
    {
    //load landscape
    }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM