Swift – 仅允许在iPad上旋转
我怎样才能允许我在iOS 8.3 SDK中使用Swift编写的通用应用程序仅支持iPhone上的肖像模式,但是iPad上的纵向模式和横向模式都是如此?
我知道在过去这已经在AppDelegate中完成。 我怎么能在Swift中做到这一点?
你可以通过编程来完成,或者更好的是,你可以简单地编辑你的项目的Info.plist(这应该更实用,因为它是一个全局的设备configuration)
只需添加“支持的接口方向(iPad)”键
你可以通过编程来完成
override func shouldAutoRotate() -> Bool { if UIDevice.currentDevice().userInterfaceIdiom == .Pad { return true } else { return false } }
接着
override func supportedInterfaceOrientations() -> Int { return UIInterfaceOrientation.Portrait.rawValue }
或者您希望默认的任何其他旋转方向。
这应该检测您使用的设备是否是iPad,并且只允许在该设备上旋转。
编辑:既然你只想在iPhone上的肖像,
override func supportedInterfaceOrientations() -> Int { if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return UIInterfaceOrientation.Portrait.rawValue } else { return Int(UIInterfaceOrientationMask.All.rawValue) } }
我知道在过去这已经在AppDelegate中完成。 我怎么能在Swift中做到这一点?
您使用的语言不会更改应用程序的体系结构。 你在Swift中的做法与Objective-C中的做法相同,即通过执行:
optional func application(_ application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
在你的应用程序委托。