如何将设备方向更改为纵向

如何将设备方向更改为纵向

本文介绍了当当前方向为面朝上或面朝下时,如何将设备方向更改为纵向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当设备的当前方向是面朝上或面朝下时,是否有可能将设备方向自动旋转到纵向?

解决方案
  [self presentViewController:[UIViewController new ] animated:NO completion:NULL]; 
[self dismissViewControllerAnimated:NO completion:NULL];

这将在您的控制器上调用 supportedInterfaceOrientations



当设备方向向上或向下,并且控制器的视图将相应调整时,只需返回 UIInterfaceOrientationMaskPortrait / p>




观察 UIDeviceOrientationDidChangeNotification s即可在收到通知时使用是面向上/向下设备方向,因为这些方向不对应于 UIInterfaceOrientation (并且不会触发


设备和接口方向定义

的typedef NS_ENUM(NSInteger的,UIInterfaceOrientation){
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} ;

typedef NS_ENUM(NSInteger,UIDeviceOrientation){
UIDeviceOrientationUnknown,
UIDeviceOrientationPortrait,//垂直设备,底部的主页按钮
UIDeviceOrientationPortraitUpsideDown, ,home按钮在顶部
UIDeviceOrientationLandscapeLeft,//面向水平,主页按钮在右边
UIDeviceOrientationLandscapeRight,//面向水平,主页按钮左边
UIDeviceOrientationFaceUp,//面向设备flat,face up
UIDeviceOrientationFaceDown //面向设备的平面,面朝下
};


Is there a possible way to autorotate the device orientation to portrait when the current orientation of the device is faceup or facedown?

解决方案

You can use this trick to make the system refresh your controller orientation anytime you want:

[self presentViewController:[UIViewController new] animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];

This will call supportedInterfaceOrientations on your controller.

Just return UIInterfaceOrientationMaskPortrait when the device orientation is facing up or down and your controller's view will get adjusted accordingly.


Observe UIDeviceOrientationDidChangeNotifications to use the trick when you receive the notification and it's a face up/down device orientation, as those orientation do not correspond to UIInterfaceOrientations (and won't trigger supportedInterfaceOrientations).

Device and interface orientation definitions:

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

这篇关于当当前方向为面朝上或面朝下时,如何将设备方向更改为纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:26