根据此处的 UIViewController
文档,
https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/clm/UIViewController/attemptRotationToDeviceOrientation
使用 [UIViewController attemptRotationToDeviceOrientation]
强制系统尝试将界面旋转到新方向.我正在尝试通过以下方式强制界面从纵向旋转到横向:
using [UIViewController attemptRotationToDeviceOrientation]
forces the system to attempt to rotate the interface to a new orientation. I am attempting to force an interface rotation from portrait to landscape by:
forceLandscape
标志[UIViewController attemptRotationToDeviceOrientation]
-supportedInterfaceOrientations
并检查标志以更改支持的方向,例如forceLandscape
flag before attempting the rotation[UIViewController attemptRotationToDeviceOrientation]
-supportedInterfaceOrientations
in my view controller
and checking a flag to change the supported orientations, e.g.这会强制再次调用 -supportedInterfaceOrientations
,看起来像
This forces -supportedInterfaceOrientations
to be called again, which looks like
- (NSUInteger)supportedInterfaceOrientations {
return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
即使 -supportedInterfaceOrientations
被调用并返回新的方向,界面也不会旋转.我已经确认该项目允许所有方向,并且该视图控制器是窗口的根视图控制器.有没有其他人遇到过这个问题并找到了解决方案?我知道解决这个问题的所有技巧(呈现和解除模式等),但如果可能的话,我想要一个干净的解决方案.我已经在 iOS 6 和 7 上验证了这个问题.
Even though -supportedInterfaceOrientations
is being called and returns the new orientation, the interface does not rotate. I have confirmed that the project allows all orientations and that this view controller is the window's root view controller. Has anyone else run into this problem and found a solution? I am aware of all the hacks to fix this (presenting and dismissing modals, etc), but I would like a clean solution if possible. I have verified this issue on iOS 6 and 7.
下面是完整的重现代码:
Below is the complete code to reproduce:
@interface ALTViewController ()
@property (nonatomic, assign) BOOL forceLandscape;
@end
@implementation ALTViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.forceLandscape = NO;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"button" forState:UIControlStateNormal];
[button sizeToFit];
[self.view addSubview:button];
button.center = self.view.center;
[button addTarget:self
action:@selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender
{
self.forceLandscape = YES;
[UIViewController attemptRotationToDeviceOrientation];
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
调用 attemptRotationToDeviceOrientation 只会旋转界面的方向,当且仅当设备本身已经旋转.
A call to attemptRotationToDeviceOrientation only rotates the orientation of the interface, if and only if, the device itself has been rotated.
例如,如果您的视图控制器仅支持纵向,而用户将设备旋转为横向,则不会发生界面旋转.当设备处于横向时,假设您的代码切换了一个允许横向的标志.界面方向会发生什么?什么都没有,因为设备方向已经发生.要使界面方向与设备方向相匹配,您需要调用尝试RotationToDeviceOrientation.
If your view controller only supported portrait orientation, for example, and the user rotated the device to landscape orientation, no interface rotation would occur. While the device was in landscape orientation, let's say your code toggles a flag which allows for landscape orientation. What would happen to the interface orientation? Nothing, because device orientation has already occurred. To get the interface orientation to match the device orientation, you would need to call attemptRotationToDeviceOrientation.
在我自己的代码中,我最近创建了一个自定义的多部分视图动画,如果中间发生界面旋转,它看起来不正确.所以我在简短的动画中关闭了界面旋转.如果用户在动画期间旋转设备,则不会出现界面方向.但是,当重新打开界面方向时,界面不会旋转以匹配设备方向,直到我调用了 attemptRotationToDeviceOrientation.
In my own code, I recently created a custom, multi-part view animation that didn't look right if interface rotation occurred in the middle of it. So I turned off interface rotation during the brief animation. If the user rotated the device during the animation, no interface orientation would occur. However, when interface orientation was turned back on, the interface would not rotate to match the device orientation until I called attemptRotationToDeviceOrientation.
这篇关于强制设备旋转 +attemptRotationToDeviceOrientation 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!