【发布时间】:2013-10-08 03:34:09
【问题描述】:
根据此处的UIViewController 文档,
使用[UIViewController attemptRotationToDeviceOrientation] 会强制系统尝试将界面旋转到新方向。我正在尝试通过以下方式强制界面从纵向旋转到横向:
- 在尝试旋转之前设置
forceLandscape标志 - 致电
[UIViewController attemptRotationToDeviceOrientation] - 在我的视图控制器中实现
-supportedInterfaceOrientations并检查标志以更改支持的方向,例如
这会强制再次调用-supportedInterfaceOrientations,看起来像
- (NSUInteger)supportedInterfaceOrientations {
return self.forceLandscape ? UIInterfaceOrientationMaskLandscapeLeft : UIInterfaceOrientationMaskPortrait;
}
即使-supportedInterfaceOrientations 被调用并返回新的方向,界面也不会旋转。我已经确认该项目允许所有方向,并且该视图控制器是窗口的根视图控制器。有没有其他人遇到过这个问题并找到了解决方案?我知道解决这个问题的所有技巧(呈现和解除模式等),但如果可能的话,我想要一个干净的解决方案。我已经在 iOS 6 和 7 上验证了这个问题。
下面是完整的重现代码:
@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;
}
【问题讨论】:
-
在您的问题中,您似乎将术语设备与术语接口混合在一起,这使得您很难理解您想要实现的目标。即使设备保持相同的方向,您是否尝试将界面旋转到新的方向?
-
直到 - 同意:) 为了清楚起见,我只是编辑了这个问题。你是对的,我试图在设备保持纵向时强制界面从纵向到横向。
标签: iphone ios cocoa-touch uiviewcontroller rotation