我的应用程序是基于导航的,主要运行在纵向,但一个视图控制器同时支持纵向和横向。现在的问题是,当我从横向视图控制器推送新的视图控制器时,新的视图控制器也被推送到横向模式,尽管我希望它处于纵向模式。
同样,当我从横向控制器弹出视图控制器时,弹出的视图控制器将以纵向模式显示。
我不知道我的代码出了什么问题。
下面是我的代码片段和用于这些方向支持的信息。
在info.plist文件中,我保留了对所有方向的支持,但肖像上下颠倒。
我还添加了导航控制器类别,如下所示。
@implementation UINavigationController(Rotation_IOS6)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end我还创建了一个UIViewController子类,它充当所有类的超类。下面是超类的定向方法。
@implementation ParentViewController
- (BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
@end支持横向的方向方法控制器如下所示。
@implementation LandscapeController
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end提前谢谢。
发布于 2013-02-15 22:45:12
下面是推送view.take an object开关的方法
在switch.m
+ (void)loadController:(UIViewController*)VControllerToLoad andRelease:(UIViewController*)VControllerToRelease
{
VControllerToLoad.navigationController.navigationBar.frame=CGRectMake(0, 0, 568, 44);
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect windowFrame = [[UIScreen mainScreen] bounds];
CGRect firstViewFrame = CGRectMake(statusBarFrame.origin.x, statusBarFrame.size.height, windowFrame.size.width, windowFrame.size.height - statusBarFrame.size.height);
VControllerToLoad.view.frame = firstViewFrame;
//check version and go
if (IOS_OLDER_THAN_6)
{
[((AppDelegate*)[UIApplication sharedApplication].delegate).window addSubview:VControllerToLoad.view];
}
else
{
[((AppDelegate*)[UIApplication sharedApplication].delegate).window setRootViewController:VControllerToLoad];
}
[VControllerToRelease.view removeFromSuperview];
}您可以使用上面的方法从横屏模式推送到人像视图。
PortraitViewController *portraitVC=[[PortraitViewController alloc]initWithNibName:@"PortraitViewController" bundle:nil];
[Switch loadController:portraitVC andRelease:self];在横向视图控制器上。m
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.title=@"Landscape";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.navigationController.navigationBarHidden = NO;
if (IOS_OLDER_THAN_6)
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:NO];
// Do any additional setup after loading the view from its nib.
}
#ifdef IOS_NEWER_OR_EQUAL_TO_6
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
#endif发布于 2013-02-15 23:07:50
我也遇到过类似的问题。在我的例子中,这个视图控制器必须始终以横向显示,并且在返回的过程中,其他视图控制器必须设置回纵向。
我在常见问题解答中找到了以下解决方案:iPhone Landscape FAQ and Solutions
这个想法的关键点是,只能通过一种方式将设备强制到某一方向: 1.设置操作杆的方向。2.以模态方式呈现任何视图控制器(!)支持目标方向的。
那么,你不想用情态的方式呈现什么吗?别担心。没有什么能阻止你……3.一旦视图控制器出现,立即将其删除。当你这样做时,它甚至对用户是不可见的。但一旦完成,你的设备就会保持在这个方向上。4.在使用故事板的情况下,推送您想要显示的视图控制器,甚至分割到它。
在我看来,这似乎是唯一可行的解决方案。除了FAQ中答案之外,还必须正确设置其他所有内容。整个项目支持的设备方向必须基本上都是all。每个视图控件支持的方向必须是每个视图控制器支持的方向,并且shouldAutoRotate应该返回YES。如果涉及到选项卡栏控制器,就会变得更加困难。如果你的应用是基于标签栏的,请联系我。
https://stackoverflow.com/questions/14896607
复制相似问题