IOS:在 UINavigationController 中锁定方向

标签 ios orientation landscape-portrait

在我的应用程序中,我启动了带有纵向导航 Controller 的第二个 View Controller ,然后在第四个 View Controller 中结束,我应该在其中旋转 iPhone 以显示图像。 现在的问题是: 如果在目标中我以这种方式设置方向: enter image description here

我可以使用这种方法来旋转我的图像:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

它工作正常,但我无法锁定属于导航 Controller 的所有 View ,因为它们都旋转纵向和横向。

第二个选项是以这种方式设置目标: enter image description here

但是这个结构不检测方向:

在 View 中会出现:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    [self willRotateToInterfaceOrientation:orientation duration:1];

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (BOOL)shouldAutorotate
{
        return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }
    }

    else{

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }

    }
}

什么是最好的解决方案? 谢谢

最佳答案

我忍不住觉得,如果我阅读了文档,它会节省我一些时间,但对我来说,解决方案是子类 UINavigaionController 并添加以下内容:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

而且因为这里真的没有任何实质性的事情发生,所以没有必要用 coder 或其他任何东西覆盖 init。您可以在之前引用导航 Controller 的任何地方引用这个子类,包括 Interface Builder。

关于IOS:在 UINavigationController 中锁定方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334765/

相关文章:

ios - 是否可以在纵向 View 上设计横向项目?

ios - Xamarin iOS 还原 PlaceHolder

ios - 如何将视频从文档目录保存到图库?

ios - 纵向模式下的横向框架

css - 如何使用 CSS 确定设备是纵向模式还是横向模式

android相机纵向方向

ios - 无法生成位码包(存档时),因为静态框架 (.framework) 是在没有完整位码的情况下构建的

ios - 在 View Controller 中绑定(bind)元组或字符串以使用 RXSwift 查看模型

java - 如何注册方向变化的监听器?

android - 什么时候不能使用 onRetainNonConfigurationInstance()