IOS7 / IOS8仅在视图控制器中允许纵向

我正在构建一个只有1个横向视图的iPhone应用程序,所以我想阻止其他所有的景观,我试过这个:

-(NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

但它仍然在旋转

我建议您只为肖像模式制作应用程序,然后在需要横向模式时再使用横向模式。

首先,如前所述,单击 – >项目名称 – >常规 – >部署信息 – >仅选择设备方向的纵向。

其次,在你的AppDelegate.h添加这个属性..

 @property (nonatomic) BOOL fullScreenVideoIsPlaying; 

然后,在你的AppDelegate.m我将添加这个function..

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{ if (self.fullScreenVideoIsPlaying == YES) { return UIInterfaceOrientationMaskAllButUpsideDown; } else { return UIInterfaceOrientationMaskPortrait; } } 

执行此操作后,在视图控制器中您需要横向创建一个函数或只是将此代码添加到您的viewWillAppear方法取决于您想要如何实现此目的。

 ((AppDelegate *)[[UIApplication sharedApplication] delegate]).fullScreenVideoIsPlaying = YES; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 

然后,为了设置回纵向模式,你这样做..

 AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.fullScreenVideoIsPlaying = NO; [self supportedInterfaceOrientations]; [self shouldAutorotate:UIInterfaceOrientationPortrait]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; 

您可能需要iOS 8的这些function..

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (BOOL)shouldAutorotate:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } -(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; } 

我希望它有帮助.. 🙂

单击项目,然后从那里选择方向设置。