gpt4 book ai didi

swift - 在 Swift iOS 中设置设备方向

转载 作者:IT王子 更新时间:2023-10-29 04:58:13 28 4
gpt4 key购买 nike

我正在为 iPhone 开发一个 swift 应用程序。我的应用程序中有一个模态视图,我只想以纵向 View 显示。

我的问题是,如何以编程方式强制手机不允许旋转?换句话说,我正在寻找不允许模态视图以横向模式显示的代码(打开纵向旋转锁定)。

这仅适用于 1 个模态视图,因此我无法关闭整个应用的旋转,否则我会完全禁用旋转。

我在研究中找到了代码 here但它在 objective-c 中,以防万一。谢谢!

最佳答案

Hi for LandscapeLeft 和 LandscapeRight (更新 Swift 2.0)

enter image description here你有这个信息

enter image description here

和 UIController

override func shouldAutorotate() -> Bool {
return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]
}

对于 PortraitUpsideDown 和 Portrait 使用那个 enter image description here

override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false
}
else {
return true
}
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]
}

来自法国的消息,圣诞快乐!

编辑:

其他解决方案:

extension UINavigationController {
public override func shouldAutorotate() -> Bool {
if visibleViewController is MyViewController {
return true // rotation
} else {
return false // no rotation
}
}

public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (visibleViewController?.supportedInterfaceOrientations())!
}
}

关于swift - 在 Swift iOS 中设置设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25651969/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com