0
votes

after browsing through stackoverflow for answers I decided to ask a question.

From my understanding, I'm supposed to override the supportedInterfaceOrientation to handle orientation. For example's sake I implemented it like this

- (NSUInteger)supportedInterfaceOrientations {
    if (self.forceLandscape) {
         return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

This lets the controller start in landscape mode when presented and get the forceLandscape ON on default. Then there's a button that will change the orientation on button press

- (IBAction)buttonPress:(id)sender {
    self.forceLandscape = !self.forceLandscape;
    UIInterfaceOrientation o = UIInterfaceOrientationPortrait;
    if (self.forceLandscape) {
       o = UIInterfaceOrientationLandscape;
    }
    [UIApplication sharedApplication].statusBarOrientation = o;
}

Button press would alternatively change between portrait and landscape mode. By setting the status bar orientation it would call the supportedInterfaceOrientations to change the orientation for me. It does call the method and return mask portrait on first button press but it doesn't change the orientation for me. This is the issue I want to fix. Hope that there's a workaround for this.

Replacing the status bar orientation change to this code

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: o] forKey:@"orientation"];

Does call supportedInterfaceMethod and it does change the orientation. However it only work once and that it has access to private code and will be rejected by Apple, which is not desirable.

1

1 Answers

0
votes

Not sure that this solution works. In my project (iOS6,7), I fixe the orientation so I don't need to force to change the orientation. However, I found a function in UIViewController that "attemp to rotate the device orientation to the right one

-(BOOL)shouldAutorotate {
    FLog(@"");
    if (self.forceLandscape) { //force to landscape
        return NO;
    } else {
        return YES; //let's application rotate it self
    }
}
-(NSUInteger)supportedInterfaceOrientations {
    if (self.forceLandscape) {
        return UIInterfaceOrientationMaskLandscapeLeft;
    } else {
        //You can just allow Portrait.
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskPortrait;
    }
}

// Notifies when rotation begins, reaches halfway point and ends.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    //save new orientation
    _myOrientation = toInterfaceOrientation;
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}

- (IBAction)buttonPress:(id)sender {
    self.forceLandscape = !self.forceLandscape;

    if (self.forceLandscape) {
        _myOrientation = UIInterfaceOrientationMaskLandscapeLeft
    } else {
        //just do nothing
    }

    //call this to update orientation
    [UIViewController attemptRotationToDeviceOrientation];
}