具有多个视图的 iPad 应用程序,如何使一个视图仅在横向模式下可见?

发布于 2024-11-19 23:05:09 字数 2227 浏览 2 评论 0原文

经过 miamk 的支持性回答后,我离实现我想要的又近了一步。但是现在有以下问题:

我有一个 iPad 应用程序,可以在所有四种视图模式下使用(纵向上/下和横向左/右) )。但在某个时刻,我有一个视图,我只想在横向模式下看到。因此,我在 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 度。所以基本上它以纵向模式显示内容,而不是使用横向模式(这实际上是您握住设备的方式)。

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

After a supportive answer of miamk, I am a step closer to achieving what I want.. But there's the following issue now:

I have an iPad application that can be used in all four view modes (portrait up/down and landscape left/right). But at a certain point I have a View that I only want to be seen in landscape mode. So I do the following in the UIViewController that will trigger the action to view the landscape-only view:

- (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;
    }
}

This works fine and thus shows either the desired screen (PropertyViewController) when the iPad is held in landscape mode, and if not it shows the RotateDeviceViewController which shows the user a message that he/she is supposed to rotate the device to correctly view the screen.

So that works!

Then the problem arises in this RotateDeviceViewController.. There I have the following:

- (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];
}

So as soon as I rotate the device (when viewing the RotateDeviceViewController) to landscape mode I show the user the PropertyViewController. This works... But when the PropertyViewController appears it shows my layout 90 degrees rotated. So basically it shows the content in portrait mode instead of using the landscape mode (which is actually the way you are holding the device)..

I hope this makes sense and someone can show me what's causing this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空名 2024-11-26 23:05:09

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

老实说,我认为当用户仍然处于纵向方向时,所有东西都已经旋转了,这很丑陋。

A more elegant workaround (at least in terms of design) to @MacN00b's answer, is to set up a portrait view with a message that tells the user that he should rotate the device and only when he rotates it you show the view built for landscape.

Honestly, I think its ugly to have everything already rotated when the user is still in portrait orientation.

圈圈圆圆圈圈 2024-11-26 23:05:09

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

[[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 包含一个标签,通知用户旋转屏幕查看内容,然后使横向视图包含您的实际内容。

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

You can listen for orientation changes using:

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

And then respond to those by loading the appropriate view...

- (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;
    }
}

If this is going into a dedicate UIView subclass for the screen in question you can make the portraitView contain a label notifying the user to rotate the screen to view the content and then make the landscape view contain your actual content.

I currently do this in an app and both views are contained in a single nib. Just be sure you set the orientation on the view properties in IB appropriately for each view...

清醇 2024-11-26 23:05:09

试试这个:

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

希望这有帮助。

Try this:

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

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文