检查 iOS 设备方向

Checking iOS device Orientation

每次我都需要获取 iOS 设备的方向 iOS 设备更改方向请记住不要在应用程序启动时或加载时,每次设备更改方向时我都需要注册它 。

您必须 Listen/observe/watch 才能更改设备方向 UIDeviceOrientationDidChangeNotification

同上question here

根据 apple UIKit Framework Reference 每次设备方向改变时调用以下方法。

对于 iOS8 及更高版本

通知容器它的视图大小即将改变。 (必填)

- (void)viewWillTransitionToSize:(CGSize)size
       withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator

参数

size - The new size for the container’s view.

coordinator - The transition coordinator object managing the size change. You can use this object to animate your changes or get information about the transition that is in progress.

讨论

UIKit calls this method before changing the size of a presented view controller’s view. You can override this method in your own objects and use it to perform additional tasks related to the size change. For example, a container view controller might use this method to override the traits of its embedded child view controllers. Use the provided coordinator object to animate any changes you make.

If you override this method in your custom view controllers, always call super at some point in your implementation so that UIKit can forward the size change message appropriately. View controllers forward the size change message to their views and child view controllers. Presentation controllers forward the size change to their presented view controller.

对于 iOS2 最多 iOS8

用户界面旋转后发送到视图控制器。

弃用声明

Use viewWillTransitionToSize:withTransitionCoordinator: to make interface-based adjustments.

声明

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

参数

fromInterfaceOrientation - The old orientation of the user interface. For possible values, see UIInterfaceOrientation.

讨论

Subclasses may override this method to perform additional actions immediately after the rotation. For example, you might use this method to reenable view interactions, start media playback again, or turn on expensive drawing or live updates. By the time this method is called, the interfaceOrientation property is already set to the new orientation. Your implementation of this method must call super at some point during its execution.

This method is called regardless of whether your code performs one-step or two-step rotations.

来源 - UIKit Framework Reference(希望对您有所帮助)