View Controllers in view Controller and orientation iOS
Asked Answered
C

2

0

I have iPAD app which has UIViewController A as root view Controller of Navigation Controller. Now i have 3 more View Controllers B,C, D as subview of ViewController A.

I want B not to respond orientation while C and D should respond to it.

Currently with code all of them respond to orientation change.

There was another answer which says make two separate root ViewControllers and add them into windows View. One of them non rotating and other rotating. I cant do that because i have header in ViewController A which switches B,C,D to make them front viewController.

Anyway please suggest.

Thanks

Counterproductive answered 12/8, 2013 at 10:58 Comment(0)
K
1

You need to subclass the UINavigationController like this.

.H

#import <UIKit/UIKit.h>

@interface UINavigationController (Rotation)

- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;

@end

.M

#import "UINavigationController+Rotation.h"

@implementation UINavigationController (Rotation)

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    if ([self visibleViewController] && [[self visibleViewController] isKindOfClass:[B class]]) {
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

@end
Kimi answered 12/8, 2013 at 11:25 Comment(7)
Is it category mate? what is (Rotation) here?Counterproductive
How can i use it mateCounterproductive
Yes, it is a category on uinavigationcontroller.Kimi
New File -> Objective-C category -> Category on :UINavigationController and name:Rotation.Kimi
It never execute first condition in supportedInterfaceOrientations .Counterproductive
If try to log it NSLog(@"%@", NSStringFromClass(self.visibleViewController.class)); it always show ViewControllerCounterproductive
Are you using UINavigationController's pushViewController to open these views or just adding by addSubview comment? If you're not pushing them, there is no easy way to rotate. You need to describe a rotation change listener and transform your view to desired orientation.Kimi
W
0

You can create subclass from UINavigationController or make category for it. And implement this methods:

-(BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation]; 
}

And then, in your controllers you should implement this methods with orientations which you want.

Waits answered 12/8, 2013 at 12:34 Comment(1)
It never call topViewController's delegate methodsCounterproductive

© 2022 - 2024 — McMap. All rights reserved.