我对iPad上的定位有一个问题。我在iPhone上禁用了方向,但我可以在某个特定的控制器上以编程方式更改它。但是在iPad上我做不到。我不知道为什么,虽然我在这个网站上搜索了一下,一些开发人员写了答案来检查UIRequiresFullScreene
,我还是这么做了。但它会禁用所有屏幕上的方向。有没有办法在iPad上改变特定控制器的方向?
下面的代码是我用来改变iPad方向的,但它不起作用。
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
感谢预支
发布于 2020-06-12 14:55:13
以编程方式设置设备的方向时。执行定向动画的函数
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
正在调用来自AppDelegate
的。
如果您将其设置为仅返回UIInterfaceOrientationMask.portrait
,您将永远看不到转换。
另外,我个人也有这些问题,为了解决这个问题,我不得不编程强制手机旋转到当前方向,然后旋转到想要的方向。
手机从纵向旋转landscapeLeft的示例
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
内部
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
}
https://stackoverflow.com/questions/62333094
复制相似问题