Search code examples
iosuiinterfaceorientationsize-classesuideviceorientation

Can a viewcontroller presented modally over current context (UIModalPresentationStyleOverCurrentContext) be rotated with device?


I have a controller which should be rotated with interface (device) orientation change. I track orientation changes, however i'm unable to rotate the view with the device orintation changes.

I can animate the views inside, and switch their constraints etc, but i'd like to use Size classes for that. When i switch to UIModalPresentationStyleCurrentContext on presenting the controller it works, but messes with my navigation and tabbar, and i would like to do it this way.

Can it be done this way?


Solution

  • Ok, solved it.

    I track device orientation changes by putting this in viewDidLoad of the controller which needs to change orientation:

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

    Then in orientationChanged:

    - (void)orientationChanged:(NSNotification *)notification{
        [RecorderViewController attemptRotationToDeviceOrientation];
    }
    

    In order for that to work in the parent controller i added these :

    -(BOOL) shouldAutorotate {
        return true;
    }
    
    -(NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
    

    Also, for rotation to work recorder controller has to implement these:

    - (BOOL)shouldAutorotate {
        return true;
    }
    
    
    
    
    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } 
    

    Hopefully somebody finds this useful.