Device orientation on only 1 view controller? in objective c

How do i programatically set a device orientation for only one view controller, and remianing all the view controller set to the protrait mode?


I have tried all the code functionalities that present in stackover flow, even though there is no luck.


I request you to give me a help on this issue.

What do you mean ? You want to force the display of one specific viewController to landscape (even if device is hold in Portrait) or you want to only allow Portrait ?


Did you try to override the supportedInterfaceOrientations property for this viewController class ?


There was a suggestion in (but could not make it work):

https://stackoverflow.com/questions/27037839/force-landscape-mode-in-one-viewcontroller-using-swift


override func viewDidLoad() {
    super.viewDidLoad()
    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return .landscape
}

override var shouldAutorotate: Bool {
    return true
}

Only one specific view controller to set for all orientations based on device rotation. and remain existing view controllers needs to be in protrait even the device has change the mode.


I need the code functionality to work in Objective C

I do not practice objc enough to be sure of proposing something.


But this seems simple enough to port to objc, isn't it ?

Check out:

https://developer.apple.com/library/archive/qa/qa1688/_index.html


Note:

  • Beginning with iOS 6, when UIKit determines that rotation may need to occur, it calls the
    shouldAutorotate
    method of the topmost view controller to determine whether to proceed with the rotation. The default implementation of this method returns
    YES
    ; however, a view controller may dynamically disable automatic rotation at runtime by overriding
    shouldAutorotate
    to return
    NO
    .
  • The view controller is a child of another view controller.Beginning with iOS 6, only the topmost view controller (alongside the
    UIApplication
    object) participates in deciding whether to rotate in response to a change of the device's orientation. More often than not, the view controller displaying your content is a child of
    UINavigationController
    ,
    UITabBarController
    , or a custom container view controller. You may find yourself needing to subclass a container view controller in order to modify the values returned by its
    supportedInterfaceOrientations
    and
    shouldAutorotate
    methods.


So the Info.plist should allow all orientations and the code in the viewControllers that don't rotate should include:


-(BOOL) shouldAutorotate{
    return NO;
}


I believe this will leave that viewController in portrait, but I'm not sure. You might have to do this


https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations?language=objc


That involves:

//  In  the viewControllers that you want to be portrait:
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

//and also in the viewDidLoad method
    [[UIDevice currentDevice] setValue:[NSNumber numberWithInt:UIDeviceOrientationPortrait] forKey:@"orientation"];
   

in your AppDelegate -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ @try { UIViewController *rootViewController = window.rootViewController;

    // Check if the root view controller is a navigation controller
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController;
        
        // Check if the top view controller of the navigation controller is valid
        if (navigationController.topViewController != nil) {
            UIViewController *topViewController = navigationController.topViewController;
            
            // Check if the top view controller is an instance of the viewcontroller you want it to be landscape
            if ([topViewController isKindOfClass:[MRFullScreenVC class]]||[topViewController isKindOfClass:[MRPlaybackFullScreenVC class]]) {
                YXHLog(@"supportedInterfaceOrientationsForWindow is Landscape");
                return UIInterfaceOrientationMaskLandscape;
            }
        }
    }
    
    YXHLog(@"supportedInterfaceOrientationsForWindow is Portrait");
    return UIInterfaceOrientationMaskPortrait;
} @catch (NSException *exception) {
    YXHLog(@"Exception caught: %@", exception);
    // Handle the exception gracefully, for example, by logging the error and returning a default value
    return UIInterfaceOrientationMaskPortrait;
}

}

Device orientation on only 1 view controller? in objective c
 
 
Q