在设备方向从纵向更改为横向时,使用新的 UIView 填充整个横向屏幕



所以我的iPhone应用程序目前有一个填充整个屏幕的tabviewcontroller。应用仅在纵向模式下运行。我的任务是检测设备方向的变化,一旦它变为横向,就会让一个新的 uiview 填充整个屏幕。

我已经有设备方向变化检测工作。我使用 NSNotificationCenter 在检测到方向更改后成功调用了帮助程序方法 deviceOrientationChanged。如果更改为横向模式,我会运行某个代码块。

在这个代码块中,我已经尝试了各种方法,但没有一个成功。简单地说self.view = newViewThing;不起作用,因为状态栏仍存在于顶部,选项卡仍存在于底部。我也尝试将这个新视图作为子视图添加到UIWindow。这不起作用,因为在添加视图时,它的方向不正确。

问题是:一旦检测到设备方向更改,有没有办法加载全新的 UIVIEW?提前谢谢你。

是的,有一种方法可以加载新视图。我在我的应用程序中以这种方式制作它:

- (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.landscapeView animated:YES];
        isShowingLandscapeView = YES;
    }
    else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
    {
        [self dismissModalViewControllerAnimated:YES];
        isShowingLandscapeView = NO;
    }    
}

并且我还将此代码添加到viewDidLoad

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

而这段代码要dealloc

[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

最新更新