我在UIViewController
中嵌入了UINavigationController
。此控制器的视图方向设置为Portait。当我在此UIViewController
上推送新视图时,仅显示横向视图,新视图也会显示为纵向,而不是它的方向景观。
我试图将UINavigationController子类化,并添加了以下方法:
- (BOOL)shouldAutorotate {
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations {
return self.topViewController.supportedInterfaceOrientations;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return self.topViewController.preferredInterfaceOrientationForPresentation;
}
在rootViewController(LoginViewController)中我做了这个:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return YES;
}
在pushViewController(一个自定义ViewController)中,我这样做了:
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotate {
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
我正在使用故事板和它们之间的推动力。我知道问题在于推送segue导致接管topviewcontroller的方向,这是纵向和pushViewController是风景。 anyboy是否知道解决方法?
非常感谢任何帮助。否则我应该放弃navVC并执行模态segue。
KR
答案 0 :(得分:2)
试试这段代码:
在AppDelegate.m类中编写以下代码。
#pragma mark Orientation Code
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientations = UIInterfaceOrientationMaskAll;
if (self.window.rootViewController) {
UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presented supportedInterfaceOrientations];
}
return orientations;
}
接下来,如果您不想要特定班级的方向,例如
停止方向viewController.m
#pragma mark Orientation
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (BOOL)shouldAutorotate
{
return NO;
}
最后改变项目目标的项目设备方向。
例如:项目目标 - >设备方向 - >选择全部(人像,向上向下,向左风景,向右风景)