关闭推送时 iOS 7 强制纵向

标签 ios xcode uinavigationcontroller orientation

此时我正在将 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;
}

最佳答案

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

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

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

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

enter image description here

希望这对您有所帮助!

关于关闭推送时 iOS 7 强制纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769753/

相关文章:

ios - 推送到 tabBar 时隐藏 View - swift

ios - 如何在 swift 中使用不同的闭包类型

ios - swift 4中datePicker中月份列中的随机值

ios - Xcode 意外删除了对文件的引用

xcode - git 与 XCode 并在 merge 时忽略 .xcodeproj?

ios - 从 Unity 到 iPhone 构建游戏(未找到 -GTMSessionFetcher 的库)?

ios - popToRootViewControllerAnimated 的问题

objective-c - 在 SplitViewController 中弹出一个 ViewController

android - 移动设备上的 JQuery 按钮 CSS

ios - iTunes Connect 无效的 SDK 值 : LC_VERSION_MIN_IPHONEOS