Parent UIViewController Orientation Shouldn't Change After Dismissing the Child

独自空忆成欢 提交于 2019-12-10 18:29:51

问题


Lets say I have three UI Controllers (A,B,C).

A is my root controller and inside the ShouldAutoRotate method I return YES. I do presentModalView from A to B (B=>inside the ShouldAutoRotate method I return Portrait) then from B I do presentModal to C (C should be able to rotate to any orientation).

Now inside C I'm able to rotate the simulator to any orientation, and the whole View rotates perfectly.Here is the problem, when C is Landscape and I dismiss it, all the objects inside B will become messed up!! same thing happens to A.

I just need to have the rotation on C!!

Gratitudes.


回答1:


In App delegate

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nonatomic) BOOL shouldRotate;
@end

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.shouldRotate == YES) {
        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
    }
    return UIInterfaceOrientationMaskPortrait;
}

In viewController A,B

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).shouldRotate = NO;
    [self supportedInterfaceOrientations];

    [self shouldAutorotate:UIInterfaceOrientationPortrait];

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

In viewController C

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    ((AppDelegate *)[[UIApplication sharedApplication] delegate]).shouldRotate = YES;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{
    return YES;

}

-(NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}

- (IBAction)closeVC:(id)sender {
    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.shouldRotate = NO;

    [self supportedInterfaceOrientations];

    [self shouldAutorotate:UIInterfaceOrientationPortrait];

    [self dismissViewControllerAnimated:YES completion:nil];
}

Hope this solves your problem




回答2:


You need to allow all orientations from project info

and then override implement all these methods to target iOS 6 + in each view controller to enable and disable orientation.

supportedInterfaceOrientations

shouldAutorotate

shouldAutorotateToInterfaceOrientation



回答3:


force rotate C to portrait before you dismiss it

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];


来源:https://stackoverflow.com/questions/11846524/parent-uiviewcontroller-orientation-shouldnt-change-after-dismissing-the-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!