强迫景观ios 7

我试图用下面的方法来强制我的观点:

- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; } - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationLandscapeLeft; } - (BOOL)shouldAutorotate{ return YES; } 

也没有工作。 请注意,我正在模拟器上和iPad上进行testing。

谢谢

这里是我如何强制我的意见之一是使用NavigationViewController景观:

  1. 实现了这个答案: https : //stackoverflow.com/a/12662433/2394787

  2. 在视图控制器中导入消息:objc / message.h

  3. 在viewDidLoad方法中添加了这一行代码:

objc_msgSend([UIDevice currentDevice],@selector(setOrientation :),UIInterfaceOrientationLandscapeLeft);

希望它可以帮助别人。

接受的答案似乎不适用于iOS 7.1.2。 (它在模拟器上工作,但在设备上运行时不起作用。)

这似乎工作,虽然:

 [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@"orientation"]; 
 int shouldShowInLandscape = 0; -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(forceToLandscape:) name:@"yourNameNotification" object:nil]; } -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return UIInterfaceOrientationMaskLandscapeLeft; } else { if(shouldShowInLandscape) { return UIInterfaceOrientationMaskLandscape; } return UIInterfaceOrientationMaskPortrait; } } -(void) forceToLandscape:(NSNotification*) theNot { shouldShowInLandscape = [[theNot object] intValue]; } 

//来自UIViewController

 - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter] postNotificationName:@"yourNameNotification" object:[NSString stringWithFormat:@"1"]]; } -(void) viewDidDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] postNotificationName:@"yourNameNotification" object:[NSString stringWithFormat:@"0"]]; } 

//删除你的通知中心