简体   繁体   English

iOS和View iOS中的View Controller

[英]View Controllers in view Controller and orientation iOS

I have iPAD app which has UIViewController A as root view Controller of Navigation Controller. 我有iPAD应用程序,该应用程序具有UIViewController A作为Navigation Controller的根视图Controller。 Now i have 3 more View Controllers B,C, D as subview of ViewController A. 现在我还有3个视图控制器B,C,D作为ViewController A的子视图。

I want B not to respond orientation while C and D should respond to it. 我希望B不响应方向,而C和D应该响应方向。

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. 还有另一个答案,说制作两个单独的根ViewController并将它们添加到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. 我不能这样做,因为我在ViewController A中具有标头,该标头切换B,C,D使它们位于viewController的前面。

Anyway please suggest. 无论如何,请提出建议。

Thanks 谢谢

You need to subclass the UINavigationController like this. 您需要像这样子类化UINavigationController。

.H 。H

#import <UIKit/UIKit.h>

@interface UINavigationController (Rotation)

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

@end

.M .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

You can create subclass from UINavigationController or make category for it. 您可以从UINavigationController创建子类或为其创建类别。 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. 然后,在控制器中,您应该以所需的方向实现此方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM