简单地说,我依靠下面的代码来提供应用程序的方向。出于多种原因在应用程序中利用了此功能:
在这种情况下,我只是负责维护工作,无法进行与现有(且功能正常)布局逻辑有所不同的重大更改。
到目前为止,它依靠以下内容来捕获应用程序方向:
var isLandscape: Bool {
return UIApplication.shared.statusBarOrientation.isLandscape
}
但是,对于Xcode 11 GM版本,我将收到以下弃用警告:
'statusBarOrientation'在iOS 13.0中已弃用:使用 而是窗口场景的interfaceOrientation属性。
如何通过状态栏获取应用程序的方向?
答案 0 :(得分:8)
Swift 5 , iOS 13 ,但与较旧的版本的iOS兼容:
extension UIWindow {
static var isLandscape: Bool {
if #available(iOS 13.0, *) {
return UIApplication.shared.windows
.first?
.windowScene?
.interfaceOrientation
.isLandscape ?? false
} else {
return UIApplication.shared.statusBarOrientation.isLandscape
}
}
}
用法:
if (UIWindow.isLandscape) {
print("Landscape")
} else {
print("Portrait")
}
答案 1 :(得分:5)
Objective C,iOS 13并与旧版本兼容:
+ (BOOL)isLandscape
{
if (@available(iOS 13.0, *)) {
UIWindow *firstWindow = [[[UIApplication sharedApplication] windows] firstObject];
if (firstWindow == nil) { return NO; }
UIWindowScene *windowScene = firstWindow.windowScene;
if (windowScene == nil){ return NO; }
return UIInterfaceOrientationIsLandscape(windowScene.interfaceOrientation);
} else {
return (UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication.statusBarOrientation));
}
}
我将其添加到UIWindow
类别中。
答案 2 :(得分:4)
Swift 5 , iPadOS 13 ,并考虑了多窗口环境:
if let interfaceOrientation = UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.windowScene?.interfaceOrientation {
// Use interfaceOrientation
}
答案 3 :(得分:3)
这是针对 Swift 5.1 的 iOS13 :
var statusBarOrientation: UIInterfaceOrientation? {
get {
guard let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation else {
#if DEBUG
fatalError("Could not obtain UIInterfaceOrientation from a valid windowScene")
#else
return nil
#endif
}
return orientation
}
}
答案 4 :(得分:2)
以上答案均不适合我,但这确实适用。 雨燕5 ioS 13.2
struct Orientation {
// indicate current device is in the LandScape orientation
static var isLandscape: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isLandscape
: (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isLandscape)!
}
}
// indicate current device is in the Portrait orientation
static var isPortrait: Bool {
get {
return UIDevice.current.orientation.isValidInterfaceOrientation
? UIDevice.current.orientation.isPortrait
: (UIApplication.shared.windows.first?.windowScene?.interfaceOrientation.isPortrait)!
}
}
}
答案 5 :(得分:1)
我提出了以下解决方案,但是如果我犯了一些我不知道的错误,则可以提出改进或建议:
var isLandscape: Bool {
return UIApplication.shared.windows
.first?
.windowScene?
.interfaceOrientation
.isLandscape ?? false
}
答案 6 :(得分:1)
Swift 5,iOS 13
guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, windowScene.activationState == .foregroundActive, let window = windowScene.windows.first else { return }
switch windowScene.interfaceOrientation {
case .unknown:
print("unknown")
case .portrait:
print("portrait")
case .portraitUpsideDown:
print("portraitUpsideDown")
case .landscapeLeft:
print("landscapeLeft")
case .landscapeRight:
print("landscapeRight")
@unknown default:
print("default")
}
答案 7 :(得分:0)
这难道不是很容易吗? (已通过Swift 5,iPadOS 13.4进行了测试)
var isLandscape: Bool {
return UIDevice.current.orientation.isLandscape
}
或基本上是:
let o = UIDevice.current.orientation
if o == .landscapeLeft || o == .landscapeRight {
// Use interfaceOrientation
}
第二个版本:
if UIDevice.current.orientation.isLandscape {
// Use interfaceOrientation
}
答案 8 :(得分:0)
UIApplication.shared.windows.first!.windowScene!.interfaceOrientation
鉴于您的应用不支持多场景。