首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >iPad面向以编程方式不起作用

iPad面向以编程方式不起作用
EN

Stack Overflow用户
提问于 2020-06-11 20:33:37
回答 1查看 120关注 0票数 2

我对iPad上的定位有一个问题。我在iPhone上禁用了方向,但我可以在某个特定的控制器上以编程方式更改它。但是在iPad上我做不到。我不知道为什么,虽然我在这个网站上搜索了一下,一些开发人员写了答案来检查UIRequiresFullScreene,我还是这么做了。但它会禁用所有屏幕上的方向。有没有办法在iPad上改变特定控制器的方向?

下面的代码是我用来改变iPad方向的,但它不起作用。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")

感谢预支

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-12 06:55:13

以编程方式设置设备的方向时。执行定向动画的函数

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask 

正在调用来自AppDelegate的。

如果您将其设置为仅返回UIInterfaceOrientationMask.portrait,您将永远看不到转换。

另外,我个人也有这些问题,为了解决这个问题,我不得不编程强制手机旋转到当前方向,然后旋转到想要的方向。

手机从纵向旋转landscapeLeft的示例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func setOrientationToLandScapeOnly() {
    appDelegate.shouldForceLandscapeOnly = true
    appDelegate.shouldForcePortraitOnly = false
    if UIDevice.current.userInterfaceIdiom == .phone {
        switch UIApplication.shared.statusBarOrientation {
            case .landscapeLeft, .landscapeRight:
                return
            default:
                APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.portrait.rawValue)
                APIOrientationHandler.shared.setDeviceValueForOrientation(UIInterfaceOrientation.landscapeLeft.rawValue)
                return
        }
    }
}

AppDelegate.swift内部

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    if shouldForcePortraitOnly {
        return UIInterfaceOrientationMask.portrait
    }

    if shouldForceLandscapeOnly {
        return UIInterfaceOrientationMask.landscape
    }

    if (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad) {
        return UIInterfaceOrientationMask.landscape
    }

    return UIInterfaceOrientationMask.portrait
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62333094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文