如何在Objective-C中修复特定视图的纵向模式?

时间:2017-07-10 01:36:35

标签: ios objective-c

我是Objective-C的新手。我想为特定视图修复肖像模式。完成视图时使用横向模式并放回纵向模式的其他视图。但是,特定视图仍然是横向模式。

- (NSUInteger) supportedInterfaceOrientations {
// Return a bitmask of supported orientations. If you need more,
// use bitwise or (see the commented return).
return UIInterfaceOrientationMaskPortrait;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
// Return the orientation you'd prefer - this is what it launches to. The
// user can still rotate. You don't have to implement this method, in which
// case it launches in the current orientation
return UIInterfaceOrientationPortrait; }

我将代码添加到特定视图。

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];

然后,此代码输入方法。

如何修改以解决此问题?

1 个答案:

答案 0 :(得分:1)

这真的很容易。 让我们有两个名为'ViewController'和'SecondViewController'的ViewController类

首先,您必须在'AppDelegate'类中导入SecondViewController。

然后在AppDelegate.m文件中复制并粘贴这两个方法

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

    if ([[self visibleViewController:[UIApplication sharedApplication].keyWindow.rootViewController] isKindOfClass:[SecondViewController class]]) {
         return UIInterfaceOrientationMaskPortrait;

     }
    return UIInterfaceOrientationMaskLandscape;
}

- (UIViewController *)visibleViewController:(UIViewController *)rootViewController {

    if (rootViewController.presentedViewController == nil) {

        return rootViewController;

    }

    if ([rootViewController.presentedViewController isKindOfClass:[UINavigationController class]]) {

        UINavigationController *navigationController = (UINavigationController *)rootViewController.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];

        return [self visibleViewController:lastViewController];

    }

    if ([rootViewController.presentedViewController isKindOfClass:[UITabBarController class]]) {

        UITabBarController *tabBarController = (UITabBarController *)rootViewController.presentedViewController;
        UIViewController *selectedViewController = tabBarController.selectedViewController;

        return [self visibleViewController:selectedViewController];
    }

    UIViewController *presentedViewController = (UIViewController *)rootViewController.presentedViewController;

    return [self visibleViewController:presentedViewController];
}

现在跑步看看。