当前方向为正面朝上或正面朝下时,如何将设备方向更改为纵向
当设备的当前方向是朝上还是朝下时,是否有可能将设备方向自动旋转到纵向?
你可以使用这个技巧让系统在任何你想要的时候刷新你的控制器方向:
[self presentViewController:[UIViewController new] animated:NO completion:NULL]; [self dismissViewControllerAnimated:NO completion:NULL];
这将在您的控制器上调用supportedInterfaceOrientations
。
当设备方向朝上或朝下时,只需返回UIInterfaceOrientationMaskPortrait
,您的控制器视图就会相应地调整。
观察UIDeviceOrientationDidChangeNotification
,以便在接收到通知时使用该技巧,因为这些方向不对应于UIInterfaceOrientation
(并且不会触发supportedInterfaceOrientations
) ,所以它是面向上/向下设备方向。
设备和界面方向定义:
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 };
你不要改变方向。 相反,您告诉iOS您支持哪些方向,因此当用户旋转设备时,iOS会将UI方向切换为您的应用程序支持的方向。 用户界面方向应该只在用户旋转设备时才会改变。
在你的 – (void)viewWillApper方法中使用这段代码
if(self.interfaceOrientation==UIInterfaceOrientationPortrait||self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { //do something; } else { }
您可以通过在项目设置(目标 – >常规 – >部署信息部分)中设置适当的值,禁用或启用纵向模式下的屏幕旋转。
- 标记肖像checkbox
- 标记颠倒的checkbox(这启用颠倒的animation)
要么
- 标记肖像checkbox
- 取消标记倒置checkbox(这只是上行模式)