Ориентация родительского UIViewController не должна меняться после отклонения дочернего элемента

Допустим, у меня есть три контроллера пользовательского интерфейса (A, B, C).

A — мой корневой контроллер, и внутри метода ShouldAutoRotate я возвращаю YES. Я делаю PresentModalView из A в B (B => внутри метода ShouldAutoRotate я возвращаю Portrait), затем из B я делаю PresentModal в C (C должен иметь возможность поворачиваться в любую ориентацию).

Теперь внутри C я могу повернуть симулятор в любую ориентацию, и весь вид вращается идеально. Вот проблема, когда C является альбомной ориентацией, и я отклоняю его, все объекты внутри B испортятся !! то же самое происходит с А.

Мне просто нужно иметь вращение на C!!

Благодарности.


person danialmoghaddam    schedule 07.08.2012    source источник
comment
Я предполагаю, что вращение должно зависеть от каждого ViewController, ориентация одного ViewController не должна влиять на другой, потому что это совершенно другой код. Вы уверены, что ваши методы shouldRotate написаны идеально?   -  person TeaCupApp    schedule 07.08.2012
comment
У меня просто есть Return YES внутри него.   -  person danialmoghaddam    schedule 07.08.2012


Ответы (3)


В делегате приложения

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

Надеюсь, это решит вашу проблему

person Karthik Rao    schedule 27.02.2015

Вам нужно разрешить все ориентации из информации о проекте

введите здесь описание изображения

а затем переопределите реализацию всех этих методов для целевой iOS 6+ в каждом контроллере представления, чтобы включать и отключать ориентацию.

supportedInterfaceOrientations

shouldAutorotate

shouldAutorotateToInterfaceOrientation
person khunshan    schedule 03.03.2015

принудительно поверните C в портрет, прежде чем закрыть его

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
person Hakim    schedule 03.03.2015