我在UINavigationController中嵌入了一个UIViewController.此控制器的视图方向设置为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
最佳答案 试试这段代码:
在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;
}
最后改变项目目标的项目设备方向.
例如:项目目标 – >设备方向 – >全选(人像,上下,左右风景,右风向)