iOS 屏幕旋轉 nav+tabbar+present(網頁) 2016

如題,最近一個app架構爲 nav + tabbar ,需求是 在點擊tabbar中的一個菜單項時,彈出網頁,該網頁須要橫屏顯示,其餘頁面不變  都保持豎屏。架構

XCode Version 7.2.1app

 

 

網上一搜,都說到在nav或者tabbar中設置如下3個方法。ide

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate
{
    return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
View Code

 

確實實現了橫屏的需求,可是發現了一個bugthis

當app在橫屏的時候 運行app  界面就是橫屏了,雖然進入關閉網頁時能夠顯示回正常豎屏,可是。。。。spa

最終 參考了一個老外的作法。傳送門code

該實現方式主要代碼爲:在AppDelegate中添加如下方法blog

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    // Get topmost/visible view controller
    UIViewController *currentViewController = [self topViewController];
    
    // Check whether it implements a dummy methods called canRotate
    if ([currentViewController respondsToSelector:@selector(canRotate)]) {
        // Unlock landscape view orientations for this view controller
        
        
        if ([currentViewController isKindOfClass:[ViewController1 class]]) {
            if(((ViewController1 *)currentViewController).isShowPortrai){
                return UIInterfaceOrientationMaskPortrait;
            }else{
                return UIInterfaceOrientationMaskLandscapeRight;
            }
        }
        return UIInterfaceOrientationMaskLandscapeRight;
    }
    
    // Only allow portrait (standard behaviour)
    return UIInterfaceOrientationMaskPortrait;
}


- (UIViewController*)topViewController {
    return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}

- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
    if ([rootViewController isKindOfClass:[UITabBarController class]]) {
        UITabBarController* tabBarController = (UITabBarController*)rootViewController;
        return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
    } else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController* navigationController = (UINavigationController*)rootViewController;
        return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
    } else if (rootViewController.presentedViewController) {
        UIViewController* presentedViewController = rootViewController.presentedViewController;
        return [self topViewControllerWithRootViewController:presentedViewController];
    } else {
        return rootViewController;
    }
    
}
View Code

 

其中改造部分爲:get

if(((ViewController1 *)currentViewController).isShowPortrai){
                return UIInterfaceOrientationMaskPortrait;
            }else{
                return UIInterfaceOrientationMaskLandscapeRight;
            }
View Code

 

完美實現此效果:進入app時豎屏,進入網頁時橫屏,關閉網頁時返回豎屏。 特此記錄it

注:只需在項目中設置下圖(默認設置),參考demo實現便可。 其中左右旋轉看需求設置io

demo地址

相關文章
相關標籤/搜索