Obj-C:视图控制器 - 多方位支持

时间:2012-05-27 07:35:08

标签: objective-c ios orientation pushviewcontroller landscape-portrait

我有一个支持肖像的家庭视图控制器。当设备将方向更改为横向时,我想将故事板中的不同视图控制器推送到屏幕。当方向变回肖像时,我想返回原始视图控制器。

这是我试过的:

在纵向视图控制器中:

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


- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsLandscape(deviceOrientation))
    {
        LandscapeView *landscapeView = [[LandscapeView alloc] init];
        [self.navigationController pushViewController:landscapeView animated:NO];
    }
}

在Landscape视图控制器中:

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

- (void)orientationChanged:(NSNotification *)notification
{
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
    if (UIDeviceOrientationIsPortrait(deviceOrientation))
    {
        [self.navigationController popViewControllerAnimated:NO];
    }
}

因此,当我在Simulator中运行此设备并旋转设备时,状态栏会改变方向,屏幕的其余部分会变为黑色且无响应。如果我旋转回肖像,它会保持黑色。为什么呢?

谢谢。

0 个答案:

没有答案