【问题标题】:UIDeviceOrientationDidChange only works with iPhone 7UIDeviceOrientationDidChange 仅适用于 iPhone 7
【发布时间】:2017-01-26 15:46:55
【问题描述】:

我正在尝试检测某个 ViewController 中的方向变化。我使用通知和自定义功能。对于 iPhone 7(运行 iOS 10.2),每次通知更改时都会调用旋转函数。但在使用 iPhone 6(运行 9.3)或 iPhone 6s(运行 10.0)时,该函数仅在屏幕加载时调用一次,而不是在我转身手机时调用。

代码:

func rotated() {
    if UIDevice.current.orientation.isLandscape {
        print("Landscape")
    } else {
        print("Portrait")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()        
    UIDevice.current.beginGeneratingDeviceOrientationNotifications()
    NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}

【问题讨论】:

  • 很有可能您在设备中锁定了旋转。从底部滑动后,此图标带有带圆圈的箭头以显示基本设置菜单。如果它被突出显示,则旋转被锁定并且不会发送任何信号,因为用户希望一切都处于纵向模式。

标签: ios swift nsnotificationcenter uiinterfaceorientation


【解决方案1】:

viewWillAppear 函数中设置通知,它应该可以正常工作。在模拟器上可用的设备上对我来说效果很好。实际上,视图 DidLoad 函数仅在视图控制器第一次出现时被调用一次。而每次要加载视图控制器时都会调用视图。

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.rotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2018-08-17
    • 2014-11-11
    • 2011-04-22
    • 2012-06-18
    • 1970-01-01
    • 2012-05-18
    相关资源
    最近更新 更多