Swift - 仅允许在 iPad 上旋转
Swift - Allow Rotation on iPad Only
如何让我在 iOS 8.3 SDK 上用 Swift 编写的通用应用程序在 iPhone 上仅支持纵向模式,但在 [=17= 上同时支持纵向和横向模式]?
我知道过去这是在 AppDelegate 中完成的。我怎么能在 Swift 中做到这一点?
您可以通过编程方式完成
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)
}
}
I know in the past this has been done in AppDelegate. How could I do this in Swift?
您使用的语言不会改变应用程序的体系结构。您在 Swift 中执行此操作的方式与在 Objective-C 中执行的方式相同,即通过实施:
optional func application(_ application: UIApplication,
supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
在您的应用程序委托中。
您可以通过编程方式完成,或者更好的是,您可以简单地编辑项目的 Info.plist(这应该更实用,因为它是全局设备配置)
只需添加"Supported Interface orientations (iPad)"键
我不确定克里斯的答案是否仅通过复制和粘贴 "Supported Interface orientations (iPad)" 密钥就可以解决;从 info.plist
的 XML 来源判断可能不是。用于实现不同方向的支持。您可以打开 info.plist
XML 源并按如下方式编辑它:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
并且 xcode UI 有一个简单的方法。转到您的项目设置。 select 您在 "General" 选项卡中的目标然后在 "Deployment Info" 部分中您可以先 select iphone/ipad 并分别标记您希望为每个设备支持的设备方向然后将设备更改为 "Universal"。它将在引擎盖下生成上面的 xml 。
此处的 "Universal" 选项显示 select 离子,它们在 iPhone 和 iPad 之间很常见。
如果您想在 Swift 4 中为特定的 ViewController 设置此项(在 iPad 上允许全部但在 iPhone 上只允许纵向):
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .pad ? UIInterfaceOrientationMask.all : UIInterfaceOrientationMask.portrait
}
如何让我在 iOS 8.3 SDK 上用 Swift 编写的通用应用程序在 iPhone 上仅支持纵向模式,但在 [=17= 上同时支持纵向和横向模式]?
我知道过去这是在 AppDelegate 中完成的。我怎么能在 Swift 中做到这一点?
您可以通过编程方式完成
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)
}
}
I know in the past this has been done in AppDelegate. How could I do this in Swift?
您使用的语言不会改变应用程序的体系结构。您在 Swift 中执行此操作的方式与在 Objective-C 中执行的方式相同,即通过实施:
optional func application(_ application: UIApplication,
supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
在您的应用程序委托中。
您可以通过编程方式完成,或者更好的是,您可以简单地编辑项目的 Info.plist(这应该更实用,因为它是全局设备配置)
只需添加"Supported Interface orientations (iPad)"键
我不确定克里斯的答案是否仅通过复制和粘贴 "Supported Interface orientations (iPad)" 密钥就可以解决;从 info.plist
的 XML 来源判断可能不是。用于实现不同方向的支持。您可以打开 info.plist
XML 源并按如下方式编辑它:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
并且 xcode UI 有一个简单的方法。转到您的项目设置。 select 您在 "General" 选项卡中的目标然后在 "Deployment Info" 部分中您可以先 select iphone/ipad 并分别标记您希望为每个设备支持的设备方向然后将设备更改为 "Universal"。它将在引擎盖下生成上面的 xml 。
此处的 "Universal" 选项显示 select 离子,它们在 iPhone 和 iPad 之间很常见。
如果您想在 Swift 4 中为特定的 ViewController 设置此项(在 iPad 上允许全部但在 iPhone 上只允许纵向):
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return UIDevice.current.userInterfaceIdiom == .pad ? UIInterfaceOrientationMask.all : UIInterfaceOrientationMask.portrait
}