此时我正在将 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;
注意:确保在部署信息中启用正确的设备方向
希望这对您有所帮助!
关于关闭推送时 iOS 7 强制纵向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769753/