在关闭子视图控制器后,父视图控制器方向不应更改

3
假设我有三个UI控制器(A,B,C)。
A是我的根控制器,在ShouldAutoRotate方法中,我返回YES。我从A到B进行presentModalView(B =>在ShouldAutoRotate方法中,我返回Portrait),然后从B到C进行presentModal(C应该能够旋转到任何方向)。
现在,在C内部,我可以将模拟器旋转到任何方向,并且整个视图都会完美地旋转。问题在于,当C处于横向时,我将其解除显示时,B内的所有对象都会变得混乱!同样的事情也发生在A中。
我只需要在C上旋转即可!!
感恩。

我猜旋转应该根据每个视图控制器而定,一个视图控制器的方向不应影响另一个视图控制器,因为它们是完全不同的代码。你确定你的 shouldRotate 方法写得完美吗? - TeaCupApp
我只是在里面写了“返回是”。 - danialmoghaddam
3个回答

1
在应用程序委托中。
@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;
}

在视图控制器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;
}

在视图控制器 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];
}

希望这能解决你的问题。

0

你需要在项目信息中允许所有方向

enter image description here

然后在每个视图控制器中覆盖实现所有这些方法,以针对iOS 6+启用和禁用方向。

supportedInterfaceOrientations

shouldAutorotate

shouldAutorotateToInterfaceOrientation

0

在关闭 C 之前强制将其旋转为竖屏

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接