首页 文章

解雇推送时,iOS 7强制纵向方向

提问于
浏览
0

此时我正在将ViewControllerA保持在纵向模式的一半 . 要做到这一点,我正在使用 preferredInterfaceOrientationForPresentation

这样可以确保ViewControllerA在推送时处于纵向模式 . 但是,如果我从ViewControllerA推送到ViewControllerB,请在ViewControllerB中切换到横向模式, and then dismiss back to ViewControllerA, ViewControllerA can be presented in landscape mode. My desire is to continue multiple orientation support of ViewControllerB, but force ViewControllerA to autorotate to portrait.

此外,出于某种原因, 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;
}

2 回答

  • 0

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

    我在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;
    

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

    enter image description here

    希望这可以帮助!

  • 1

    好吧,我能让事情奏效!

    因此,在我的情况中,让事情变得困难的一件事是我有两个部分;一个是在登录之前发生的(没有NavController)另一个是登录后(确实有NavController) . 也许我在那里打破了最佳实践指南?

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

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

相关问题