强制设置横向在xib中不起作用

我有以下代码

#define degreesToRadian(x) (M_PI * (x) / 180.0) if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ [UIView beginAnimations:@"View Flip" context:nil]; [UIView setAnimationDuration:0.5f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; self.view.transform = CGAffineTransformIdentity; self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(0)); self.view.bounds = CGRectMake(0.0f, 0.0f, 1004, 768.0f); // self.view.center = CGPointMake(160.0f, 240.0f); [UIView commitAnimations]; } 

此代码适用于强制设置横向方向

 -(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } 

我在viewdidload中使用以下代码。这个代码在测试应用程序中工作正常但在我的mainapp中。

 if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),UIInterfaceOrientationLandscapeLeft); } } 

如果我编写测试应用程序它工作正常但是在我的Mainapp中它没有工作。我在我的Mainapp中有xib

确保您已为Mainapp应用程序设置了横向模式,

在此处输入图像描述

编辑

在Printview页面ios5和6中使用以下方法

 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { if (UIInterfaceOrientationIsLandscape( interfaceOrientation)) { return YES; } return NO; } 

对于ios 7

 -(BOOL)shouldAutorotate { if (UIInterfaceOrientationIsLandscape( [[UIDevice currentDevice] orientation])) { return YES; } else return NO; } 

在项目设置中,您需要检查应用程序中应该可用的所有方向(即使您只使用一次)。

然后在最顶级的对象上,通常是(自定义)UITabBar或(自定义)UINavigationBar,您必须使用这些方向方法实现一些逻辑:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation - (NSUInteger)supportedInterfaceOrientations 

如果你有关于轮换的更详细的教程或答案,它认为它是最好的。

iOS6 +旋转

我不知道您的项目是否也支持iOS5,但该版本处理旋转不同。

我在didRotateFromInterfaceOrientation中编写了以下代码:

 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation]; NSLog(@"orientation %d",orientaiton); if(UIInterfaceOrientationIsPortrait(orientaiton)){ if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { if(orientaiton == UIInterfaceOrientationPortrait) objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeLeft); else if (orientaiton == UIInterfaceOrientationPortraitUpsideDown) objc_msgSend([UIDevice currentDevice], @selector(setOrientation:), UIInterfaceOrientationLandscapeRight); } } } 

我可以旋转到landscape.but问题是视图旋转到肖像然后返回到landscape.But我不想那样。我想修复视图到景观。

首先,您将从.xib设置此项 在此处输入图像描述

然后为该特定视图编写此方法

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeRight; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight); } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; } 

并为另一个类/视图写所有的方向。

注意: – 您需要在所有类中编写这些方法。