我想知道如何在Swift中获取当前设备方向?我知道有Objective-C的示例,但我无法让它在Swift中工作。
我正在尝试获取设备方向并将其放入if语句中。
这是我遇到最大问题的那一行:
[[UIApplication sharedApplication] statusBarOrientation]
我想知道如何在Swift中获取当前设备方向?我知道有Objective-C的示例,但我无法让它在Swift中工作。
我正在尝试获取设备方向并将其放入if语句中。
这是我遇到最大问题的那一行:
[[UIApplication sharedApplication] statusBarOrientation]
Swift 5 - 解决方案: 在应用启动时和设备旋转期间检查方向:
// app start
override func viewDidAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let orientation = self.view.window?.windowScene?.interfaceOrientation {
let landscape = orientation == .landscapeLeft || orientation == .landscapeRight
}
}
// on rotation
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
let landscape = UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight
}
windows
弃用警告。 - Jav SoloUIDevice.current.orientation在我的指南针类型应用程序中无法工作,因为它报告手机水平时显示“面朝上”或“面朝下”,而不管屏幕的方向如何。我想显示用户的方向,假设他们以自然方向拿着手机。
UIApplication.shared.statusBarOrientation给了我一个已弃用的警告“'statusBarOrientation'在iOS 13.0中被弃用:改用窗口场景的interfaceOrientation属性。”
以下内容符合我的需求。
var headingOffset = 0.0
let scenes = UIApplication.shared.connectedScenes
let windowScene = scenes.first as? UIWindowScene
switch windowScene?.interfaceOrientation {
case .portrait:
headingOffset = 0.0
print("Reporting orientation as portrait")
case .portraitUpsideDown:
headingOffset = 180.0
print("Reporting orientation as portraitUpsideDown")
case .landscapeLeft:
headingOffset = -90.0
print("Reporting orientation as landscapeLeft")
case .landscapeRight:
headingOffset = 90.0
print("Reporting orientation as landscapeRight")
default:
headingOffset = 0.0
print("Reporting orientation as default")
}
我发现Swift中可以用另一种代码替代Obj-C中的代码
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
是
if UIApplication.shared.statusBarOrientation.isLandscape
基于Rob的回答,Swift 3
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if (size.width / size.height > 1) {
print("landscape")
} else {
print("portrait")
}
}
super.viewWillTransition(to: size, with: coordinator)
。 - Bocaxica override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
if (toInterfaceOrientation.isLandscape) {
NSLog("Landscape");
}
else {
NSLog("Portrait");
}
}
Swift 5
适用于SwiftUI和基于storyboard的应用程序。同时,检查旋转和特征处理程序:
struct Orientation {
/// true - if landscape orientation, false - else
static var isLandscape: Bool {
orientation?.isLandscape ?? window?.windowScene?.interfaceOrientation.isLandscape ?? false
}
/// true - if portrait orientation, false - else
static var isPortrait: Bool {
orientation?.isPortrait ?? (window?.windowScene?.interfaceOrientation.isPortrait ?? false)
}
/// true - if flat orientation, false - else
static var isFlat: Bool {
orientation?.isFlat ?? false
}
/// valid orientation or nil
static var orientation: UIDeviceOrientation? {
UIDevice.current.orientation.isValidInterfaceOrientation ? UIDevice.current.orientation : nil
}
/// Current window (for both SwiftUI and storyboard based app)
static var window: UIWindow? {
guard let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window else {
return UIApplication.shared.windows.first
}
return window
}
}
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
layoutAll()
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
print("viewWillTransition")
layoutAll()
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
print("traitCollectionDidChange")
layoutAll()
}
/// Layout content depending on the orientation
private func layoutAll() {
// Layout as you need
print("layoutAll: isLandscape=\(Orientation.isLandscape), isPortrait=\(Orientation.isPortrait), traitCollection=\(traitCollection)")
}
}
对于任何在iOS 13之后看到这篇文章的人:
目前最可靠的方法已经被弃用,但仍然可以使用:
print(UIApplication.shared.statusBarOrientation.isPortrait)
if UIApplication.shared.windows.first?.
windowScene?.interfaceOrientation.isPortrait ?? true {
print("Portrait")
} else {
print("Landscape")
}
我认为在现代的Objective C中,考虑到现在已经弃用的函数,最好的方法是...
UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation]);
UIDevice.current.orientation.isLandscape
保持简单:
let orientation = UIApplication.shared.statusBarOrientation.isLandscape ? "landscape" : "portrait"
horizontalSizeClass
和verticalSizeClass
:import SwiftUI
struct DemoView: View {
@Environment(\.horizontalSizeClass) var hSizeClass
@Environment(\.verticalSizeClass) var vSizeClass
var body: some View {
VStack {
if hSizeClass == .compact && vSizeClass == .regular {
VStack {
Text("Vertical View")
}
} else {
HStack {
Text("Horizontal View")
}
}
}
}
}