應用內特定界面支持橫屏展示實現

在AppDelegate內實現函數如下:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if ( [[sharedNavigator topViewController] isKindOfClass:NSClassFromString(@"SLWebViewController")] )
        return UIInterfaceOrientationMaskAllButUpsideDown;
    
    return UIInterfaceOrientationMaskPortrait;
}

SLWebViewController就是你需要支持橫屏展示的ViewController。

在SLWebViewController中實現如下接口以支持除UpsideDown外的顯示方式:

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

添加UIDeviceOrientationDidChangeNotification監聽,橫屏時隱藏狀態欄和導航欄,最大化顯示高度。

#pragma mark - Reg & Unreg ntf
- (void)regNtf
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDeviceOrientationDidChangeNotification:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)unregNtf
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}

#pragma mark - Ntf handler
- (void)onDeviceOrientationDidChangeNotification:(NSNotification *)notification
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if ( orientation == UIDeviceOrientationPortraitUpsideDown ) return;
    if ( orientation == UIDeviceOrientationPortrait )
    {
        [[UIApplication sharedApplication] setStatusBarHidden:NO];
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    else if (orientation == UIDeviceOrientationLandscapeRight)
    {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    else if ( orientation == UIDeviceOrientationLandscapeLeft )
    {
        [[UIApplication sharedApplication] setStatusBarHidden:YES];
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章