overAiding shouldAutorotate不在Swift 3中工作

时间:2016-11-04 00:40:00

标签: ios swift autorotate

我试图阻止一个UIViewController的轮换,我无法实现这一目标。

我做这样的事情:

open override var shouldAutorotate: Bool {
    get {
        return false
    }
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait
    }
}

UIViewControler静止旋转。 UIViewController位于以模态方式打开的UINavigationController中。

我从这里看了很多问题,没有一个答案适合我。

在Swift 2中我曾经覆盖shouldAutorotate,但在Swift 3中,该函数不再存在。

我如何在Swift 3中做到这一点,我曾经在Swift 2中做过什么?

5 个答案:

答案 0 :(得分:39)

我不知道如果我可以多次重现这种行为,为什么要投票来结束这个问题。 UIViewController位于模拟打开的UINavigationController内。

这就是我为解决问题所做的工作。

我创建了这个类,并设置为UINavigationController,其中包含我要阻止旋转的UIViewController

class NavigationController: UINavigationController { 

    override var shouldAutorotate: Bool {
        return false
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

}

就是这样,它对我有用

答案 1 :(得分:8)

将此代码添加到AppDelegate.swift

var orientationLock = UIInterfaceOrientationMask.all
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return self.orientationLock
}

struct AppUtility {
    static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
        if let delegate = UIApplication.shared.delegate as? AppDelegate {
            delegate.orientationLock = orientation
        }
    }

    static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
        self.lockOrientation(orientation)
        UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
    }
}

添加到想要强制方向的viewcontroller:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    //let value = UIInterfaceOrientation.landscapeLeft.rawValue
    //UIDevice.current.setValue(value, forKey: "orientation")


    AppDelegate.AppUtility.lockOrientation(.landscapeLeft)

}

答案 2 :(得分:3)

对我来说,投票的答案无效。相反,

class Example(db.Model):
     title = db.Column(db.String(140))
     extra = db.Column(db.String(140))

e1 = Example(title='foo', extra='bar')
e2 = Example(title='hoo', extra='bar')

db.session.add_all([e1, e2])
db.session.commit()

test = []
for value in Example.query.distinct(Example.extra):
    test.append(value.extra)

print(len(test))
...2

这些代码有效。已在Swift 4中确认。

答案 3 :(得分:2)

我发现iOS 11根本没有调用这些方法,除非我在UIApplicationDelegate类中实现了以下AppDelegate方法:

application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?)

答案 4 :(得分:0)

iOS 11,Swift 5

新的秘诀是将其添加到viewDidLoad()

df['Times'] = df['Times'].apply(lambda x: [d.strftime('%Y-%m-%d %H:%M:%S') for d in x])

还请确保重写preferredInterfaceOrientationForPresentation以返回您的首选方向:

.dropdown { background-color: #ddd; list-style: none; padding: 10px 18px; width:max-content; margin-top:40px; } .dropdown li { border:1px solid #bbb; } a:hover, a:visited, a:link, a:active { text-decoration: none; font-family:arial; font-size:12px; font-weight:bold; color: #000; background-color:#fff; display: block; padding:4px; text-align:center; } .dropdown a.current { background-color: #eee; }

在这里找到答案: https://nunoalexandre.com/2018/08/24/forcing-an-orientation-in-ios