【发布时间】: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