use*_*428 68 orientation ios swift
我想知道如何在Swift中获得当前的设备方向?我知道有一些Objective-C的例子,但我还是无法在Swift中使用它.
我试图获取设备方向并将其放入if语句中.
这是我遇到的最多问题:
[[UIApplication sharedApplication] statusBarOrientation]
Mig*_*uel 64
您可以使用:
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.currentDevice().orientation{
    case .Portrait:
        text="Portrait"
    case .PortraitUpsideDown:
        text="PortraitUpsideDown"
    case .LandscapeLeft:
        text="LandscapeLeft"
    case .LandscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}
SWIFT 3更新
override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
    var text=""
    switch UIDevice.current.orientation{
    case .portrait:
        text="Portrait"
    case .portraitUpsideDown:
        text="PortraitUpsideDown"
    case .landscapeLeft:
        text="LandscapeLeft"
    case .landscapeRight:
        text="LandscapeRight"
    default:
        text="Another"
    }
    NSLog("You have moved: \(text)")        
}
要么
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
}
通知您可以检查:IOS8 Swift:如何检测方向变化?
注意: didRotateFromInterfaceOrientation已弃用使用 viewWillTransitionToSize for iOS 2.0及更高版本
Mik*_*e S 51
要获得状态栏(及其UI)方向,例如您拥有的Objective-C代码,它只是:
UIApplication.sharedApplication().statusBarOrientation
您还可以使用UIDevice 的orientation属性:
UIDevice.currentDevice().orientation
但是,这可能与您的UI所处的方向不匹配.来自文档:
属性的值是一个常量,表示设备的当前方向.此值表示设备的物理方向,可能与应用程序用户界面的当前方向不同.有关可能值的说明,请参阅"UIDeviceOrientation".
zSp*_*awl 25
Apple最近摆脱了Landscape vs. Portrait的想法,并且更喜欢我们使用屏幕尺寸.但是,这有效:
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("landscape")
    } else {
        print("portrait")
    }
}
Ern*_*eng 20
struct DeviceInfo {
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.statusBarOrientation.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.statusBarOrientation.isPortrait
        }
    }
}}
swift4回答:我就是这样做的,
1.适用于各种视图控制器
2.当用户旋转应用程序时也可以工作
3.也是第一次安装应用程序
Usm*_*sar 13
要查找当前的设备方向,只需使用以下代码:
UIApplication.sharedApplication().statusBarOrientation
对于swift 3.0
UIApplication.shared.statusBarOrientation
FBC*_*FBC 13
斯威夫特4:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.current.orientation.isLandscape {
            print("landscape")
        } else {
            print("portrait")
        }
    }
因此,如果Apple贬低整个方向字符串("肖像","横向"),那么您所关心的只是宽度与高度的比率.(有点像@bpedit的回答)
当您将宽度除以高度时,如果结果小于1,则主屏幕或容器或任何处于"纵向""模式".如果结果大于1,那就是"风景"画.;)
override func viewWillAppear(animated: Bool) {
    let size: CGSize = UIScreen.mainScreen().bounds.size
    if size.width / size.height > 1 {
        print("landscape")
    } else {
        print("portrait")
    }
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if size.width / size.height > 1 {
        print("landscape")
    } else {
        print("portrait")
    }
}
(我猜测如果你使用这种方法,那么当比率正好为1,宽度和高度相等时,你可能并不真正关心具体处理条件.)
statusBarOrientation被弃用,因此没有再提供在使用像上述的答案
在这个代码中可以得到方向而不用担心折旧。Swift 5 ios 13.2测试 100%
您的应用程序应允许纵向和横向工作以使用以下代码,否则结果会有所不同
windows.first是主窗口 windows.last是您当前的窗口
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)!
        }
    }
}
Swift 5 \xe2\x80\x93 解决方案:检查应用程序启动时和设备旋转期间的方向:
\n// app start\noverride func viewDidAppear(_ animated: Bool) {\n    super.viewWillAppear(animated)\n    if let orientation = self.view.window?.windowScene?.interfaceOrientation {\n        let landscape = orientation == .landscapeLeft || orientation == .landscapeRight\n    }\n}\n\n// on rotation\noverride func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {\n    super.viewWillTransition(to: size, with: coordinator)\n    let landscape = UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight\n}\n我有使用问题InterfaceOrientation,它工作正常,除了它没有访问加载方向.所以我尝试了这个,这是一个守门员.这是有效的,因为bounds.width它总是参考当前的方向而不是nativeBounds.width绝对的方向.
    if UIScreen.mainScreen().bounds.height > UIScreen.mainScreen().bounds.width {
        // do your portrait stuff
    } else {    // in landscape
        // do your landscape stuff
    }
我把这种从willRotateToInterfaceOrientation(toInterfaceOrientation:
        UIInterfaceOrientation, duration: NSTimeInterval)从viewDidLoad,但它的灵活性.
感谢zSprawl指向该方向的指针.我应该指出,这只适用于iOS 8及更高版本.
小智 6
迅捷3+
基本上:
NotificationCenter.default.addObserver(self, selector: #selector(self.didOrientationChange(_:)), name: .UIDeviceOrientationDidChange, object: nil)
@objc func didOrientationChange(_ notification: Notification) {
    //const_pickerBottom.constant = 394
    print("other")
    switch UIDevice.current.orientation {
        case .landscapeLeft, .landscapeRight:
            print("landscape")
        case .portrait, .portraitUpsideDown:
            print("portrait")
        default:
            print("other")
    }
}
:)
小智 6
UIDevice.current.orientation 不适用于我的指南针类型应用程序,因为当手机水平时,无论屏幕方向如何,它都会报告“faceUp”或“faceDown”。我想显示用户所面对的方向,假设他们以自然方向握住手机。
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")
}
| 归档时间: | 
 | 
| 查看次数: | 67592 次 | 
| 最近记录: |