解雇子项后,父UIViewController方向不应更改 [英] Parent UIViewController Orientation Shouldn't Change After Dismissing the Child

查看:89
本文介绍了解雇子项后,父UIViewController方向不应更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有三个UI控制器(A,B,C)。

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

A是我的根控制器,在ShouldAutoRotate方法中我返回YES。
我做的是从A到B的ModalView(B =>在ShouldAutoRotate方法中我返回Portrait)然后从BI执行presentModal到C(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).

现在在C里面我能够将模拟器旋转到任何方向,整个视图完全旋转。这就是问题,当C是Landscape并且我解雇它时,B里面的所有对象都会变成弄乱!!同样的事情发生在A。

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.

我只需要在C上进行轮换!!

I just need to have the rotation on C!!

感恩。

推荐答案

在App代理中

@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;
}

在viewController A中,B

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;
}

在viewController C中

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];
}

希望这能解决您的问题

这篇关于解雇子项后,父UIViewController方向不应更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆