【问题标题】:iPad app with multiple views, how do I make ONE single view viewable in landscape only?具有多个视图的 iPad 应用程序,如何使一个视图只能在横向中查看?
【发布时间】:2011-07-11 13:09:44
【问题描述】:

在得到miamk的支持后,我离实现我想要的又近了一步。但是现在有以下问题:

我有一个 iPad 应用程序,可以在所有四种视图模式(纵向上/下和横向左/右)下使用。但在某个时刻,我有一个视图,我只想在 landscape 模式下看到。所以我在 UIViewController 中执行以下操作,这将触发查看纯横向视图的操作:

- (void) showProperty:(Property *) property {
    if ([self interfaceOrientation] == UIInterfaceOrientationLandscapeLeft || [self interfaceOrientation] == UIInterfaceOrientationLandscapeRight) {
        PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
        propertyView.property     = property;
        [self.navigationController pushViewController:propertyView animated:YES];
        [propertyView release];
        propertyView = nil;
    }
    else {
        RotateDeviceViewController *rotateView = [[RotateDeviceViewController alloc] initWithNibName:@"TabRotate" bundle: [NSBundle mainBundle]];
        rotateView.property = property;
        [self.navigationController pushViewController:rotateView animated:YES];
        [rotateView release];
        rotateView = nil;
    }
}

这可以正常工作,因此当 iPad 处于横向模式时会显示所需的屏幕 (PropertyViewController),如果不是,它会显示 RotateDeviceViewController,它会向用户显示他/她应该正确旋转设备的消息查看屏幕。

这样就行了!

那么问题就出现在这个RotateDeviceViewController中了。我有以下内容:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
        [self showProperty];
    return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

- (void) showProperty {
    PropertyViewController *propertyView = [[PropertyViewController alloc] initWithNibName:@"PropertyViewController" bundle:[NSBundle mainBundle]];
    propertyView.property     = property;
    [self.navigationController pushViewController:propertyView animated:YES];
    [propertyView release];
}

因此,当我将设备(查看 RotateDeviceViewController 时)旋转到横向模式时,我会向用户显示 PropertyViewController。这行得通...但是当 PropertyViewController 出现时,它显示我的布局旋转了 90 度。所以基本上它以 portrait 模式显示内容,而不是使用 landscape 模式(这实际上是您拿着设备的方式)..

我希望这是有道理的,有人可以告诉我是什么原因造成的。

【问题讨论】:

标签: objective-c xcode ipad orientation landscape


【解决方案1】:

对于@MacN00b 的回答,一个更优雅的解决方法(至少在设计方面)是设置一个纵向视图,其中包含一条消息,告诉用户他应该旋转设备,并且只有当他旋转设备时才会显示视图专为景观设计。

老实说,当用户仍处于纵向时,我认为所有内容都已旋转很难看。

【讨论】:

  • +1 是的,你是对的。这将是一个更好的解决方案。那么,我该如何为此设置两个不同的视图呢?一个纵向显示,一个横向显示。我该如何实现呢? =/(我想我也会在 Google 上找到这个)
  • UIViewController 有一个名为interfaceOrientation 的属性,它是只读的。您可以编写一个 if 条件来加载纵向视图和横向视图。
  • 谢谢。这真的很有帮助,但现在我有另一个问题。我相应地更新了我的问题。希望你能帮助我!
【解决方案2】:

您可以使用以下方法监听方向变化:

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

然后通过加载适当的视图来响应这些...

- (void) orientationChanged:(id)object
{
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation];

    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view = self.portraitView;
    }
    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        self.view = self.landscapeView;
    }
}

如果这将进入相关屏幕的专用 UI​​View 子类,您可以让 PortraitView 包含一个标签,通知用户旋转屏幕以查看内容,然后让横向视图包含您的实际内容。

我目前在一个应用程序中执行此操作,并且两个视图都包含在一个 nib 中。只要确保在 IB 中为每个视图正确设置视图属性的方向...

【讨论】:

    【解决方案3】:

    试试这个:

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

    希望这会有所帮助。

    【讨论】:

    • -1。 Jules 明确指出,重写此方法并不能提供他要求的解决方案。
    • 确实,正如我所说,这与我在上面所说的代码中的完全一样。但这只是阻止视图从旋转到纵向模式。但如果查看器已经处于纵向模式,则不会旋转视图。
    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 2014-03-14
    相关资源
    最近更新 更多