【问题标题】:iOS 7 Force Portrait Orientation When Dismissing PushiOS 7 在关闭推送时强制纵向方向
【发布时间】:2014-09-10 15:48:24
【问题描述】:

此时,我已将 ViewControllerA 保持在纵向模式中。为此,我使用 preferredInterfaceOrientationForPresentation

这可确保 ViewControllerA 在我推送到它时处于纵向模式。但是,如果我从 ViewControllerA 推送到 ViewControllerB,在 ViewControllerB 中切换到横向模式,然后关闭返回到 ViewControllerA,ViewControllerA 可以以横向模式呈现。我的愿望是继续支持 ViewControllerB 的多方向,但强制 ViewControllerA 自动旋转为纵向。

此外,出于某种原因,shouldAutorotate 似乎没有在 ViewControllerA 中被调用。也许解决这个问题可以解决整个问题?

UINavigationController+Orientation.h 类别

@interface UINavigationController (Orientation)

- (BOOL)shouldAutorotate;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end

UINavigationController+Orientation.m 类别

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

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

@end

ViewControllerA.m

- (BOOL)shouldAutorotate {
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

【问题讨论】:

    标签: ios xcode uinavigationcontroller orientation


    【解决方案1】:

    我遇到了同样的问题。我的解决方案如下,它在一个已发布的应用程序中。

    我在 AppDelegate.h 中添加了一个名为 allowViewRotation 的公共属性:

    @property (assign, nonatomic) BOOL allowViewRotation;
    

    然后我在我的 AppDelegate.m 文件中实现了以下调用:

    - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { // if iPad
            return UIInterfaceOrientationMaskAll;
        }
    
        if (self.allowViewRotation) {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
        else {
            return UIInterfaceOrientationMaskPortrait;
        }
    }
    

    现在在 ViewControllerA 中将 allowViewRotation 属性设置为 NO

    ((AppDelegate*)[UIApplication sharedApplication].delegate).allowViewRotation = NO;
    

    并在 ViewControllerB 中将 allowViewRotation 属性设置为 YES

    ((AppDelegate*)[UIApplication sharedApplication].delegate).allowViewRotation = YES;
    

    注意:确保在部署信息中启用正确的设备方向

    希望这会有所帮助!

    【讨论】:

    • 它似乎仍然无法正常工作。我使用的方向设置和你一样(除了倒置)。我尝试在 viewDidLoad 和 viewWillAppear 中都使用 ((AppDelegate*)[UIApplication sharedApplication].delegate).allowViewRotation,但两者都没有阻止 ViewControllerA 在解除后加载为横向。
    • 确保默认值为 allowViewRotation=NO。并将 YES/NO 更改放入 ViewControllerB 的 viewWillAppear:viewWillDisappear: -(void) viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; appDelegate.allowViewRotation = YES; } -(void) viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; appDelegate.allowViewRotation = NO; }
    • 尝试调用 attemptRotationToDeviceOrientation 来强制应用调用 supportedInterfaceOrientationsForWindow developer.apple.com/library/ios/documentation/uikit/reference/…
    • 感谢所有快速回复!我尝试调用 attemptRotationToDeviceOrientation,但再次无济于事。至于 viewWillAppearviewWillDisappear*,我已经尝试过您提到的设置以及它的变体。然而它似乎并没有通过。如果 ViewControllerA 处于横向(我不想要),如果手机移动到该方向,它将切换到纵向,然后从那里锁定它。但这是目前我能得到的最接近的。
    • 是的,曾经调用 UIDevice 来强制改变方向,但这是一个私人调用,最近在 iOS7 中不再存在。抱歉,我不能提供更多帮助。
    【解决方案2】:

    好的,我能够让事情正常进行!

    所以在我的情况下让事情变得困难的一件事是我有两个部分;一个发生在登录之前(没有 NavController),另一个是登录后(有 NavController)。也许我在那里违反了最佳实践指南?

    无论如何,Teo C. 的答案是帮助我的答案:iOS 7. Change page orientation only for one view controller

    我通过创建一个新的空白项目来发现这一点。如果您在强制定向时遇到问题,可以查看我制作的示例 https://github.com/jerherrero/Force-Orientations

    【讨论】:

      猜你喜欢
      • 2014-10-13
      • 2013-01-15
      • 1970-01-01
      • 2020-10-24
      • 2010-12-21
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多