在iOS 7中强制使用Landscape ViewController

Fed*_*olo 4 objective-c ios

我在纵向模式下有十多个ViewControllers,但无论设备的方向如何,我都需要在横向模式下强制使用一个.

Fed*_*olo 12

这是解决方案:

1)将LandscapeViewController嵌入子类NavigationController中,并使用模态segue从PortraitViewController连接它.

LandscapeViewController嵌入到一个新的LandscapeNavigationController中

2)子类UINavigationController

LandscapeNavigationController.m

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}
Run Code Online (Sandbox Code Playgroud)

3)不要忘记解雇你的模态VC(在这种情况下从我的Bar Buttom Item Action)

- (IBAction)goBack:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

  • 使用这样的类别是一个可怕的想法 - 它关于碰撞发生的事件的无证行为.在使用类别时,应始终使用前缀方法来避免这种情况.在这种情况下对导航控制器进行子类化将是更好的解决方案. (3认同)
  • 这些类别的要点是覆盖.但我会选择一个子类. (2认同)