旋转 iPhone 时更改视图? [英] Change views when iPhone is rotated?

查看:47
本文介绍了旋转 iPhone 时更改视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当 iPhone 处于标准的纵向"方向时,我如何获得一个视图,并在旋转到横向时切换到不同的视图(比如图表或其他东西),反之亦然?

How would I have one view up when the iPhone is in a standard 'portrait' orientation, and switch to a different view (say a graph or something) when rotated to a landscape orientation and visa versa?

推荐答案

禁用该视图的方向(假设第一个视图是横向)

Disable orientation for that view (assuming that the first view is landscape)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft)||(interfaceOrientation == UIInterfaceOrientationLandscapeRight); }

然后将此添加到 viewDidAppear

Then add this to the viewDidAppear

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

并在某处添加此方法

- (void) orientationChanged:(NSNotification *)note
{
    UIDevice * device = note.object;
    switch(device.orientation)
    {
        case UIDeviceOrientationPortrait:
            // Present View Controller here
            break;
        default:
            break;
    };
}

在另一个视图上做同样的事情,但向后使用横向关闭而不是纵向显示.

And on the other view do the same but backwards with a dismiss for landscape instead of the present for portrait.

不要忘记取消注册通知.

Dont forget to unregister for the notifications.

(或者使用带有两个控件但没有栏的导航视图,并根据使用的方向简单地显示您想要的一个)

(alternatively use a navigation view with both controls but without the bar and simply show the one you want depending on the orientation using)

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration

这篇关于旋转 iPhone 时更改视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆