【发布时间】:2014-10-27 10:08:14
【问题描述】:
编辑:我已经更新了问题和标题。似乎问题在于,在我自己的 init 中添加的 UIView 实际上除了旋转之外没有做任何事情。我真的必须处理 UIView 的旋转吗?我试过[self.overlay addConstraints:[self.view constraints]]; 没有运气。我意识到这可能会在viewDidAppear 中使用[self.overlay setFrame:self.view.frame]; 来解决,但我宁愿永远不要在该方法中执行任何事情。
到目前为止,我一直使用 Storyboard 来设计我的视图控制器。这一次,我只是简单地编写了几行代码来显示一个视图,但似乎在控制器的 init 中创建的视图不会考虑我的方向。使用 Storyboard 并按名称等实例化 ViewController,它可以工作。现在,我自己实例化它,使用自定义的 init 方法,我认为这是问题所在。我的自定义 ViewController 只是有这个 init 方法和一些旋转代表:
-(id)initWithURL:(NSURL*)url
{
self = [super init];
if(self)
{
self.overlay = [[UIView alloc] initWithFrame: self.view.frame];
self.overlay.backgroundColor = [UIColor grayColor];
[self.view addSubview:self.overlay];
self.url = url;
}
return self;
}
/* viewDidLoad etc.. */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
为了以模态方式显示,我在当前显示的控制器中使用了它:
CustomViewController *cvc = [[CustomViewController alloc] initWithURL: url];
[cvc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:cvc animated:YES completion:nil];
(或任何其他过渡样式)
结果如下图所示:
灰色区域是在 init 中创建的 CustomViewController 中的新 UIView。黑色区域为self.view,并正确对齐。如果我允许纵向和旋转,它会像纵向一样完美对齐,但永远不会横向对齐。似乎呈现的模态 customViewController 中的 self.overlay 不会正确旋转。 我是否通过替换任何 init 方法或其他方法来覆盖某些规则?通过情节提要执行此操作时,它会以横向正确显示。
【问题讨论】:
-
尝试从 shouldAutorotate 返回 NO。
-
@danh 谢谢,但这不是问题的一部分。即使我删除了所有关于旋转的方法(
initWithURL以外的所有方法),它仍然会发生。唯一的区别是它可以正确显示纵向,但不能正确显示横向。实际上,即使设备和控制器处于横向状态,UIViewController 内的 UIView 似乎也保持纵向。
标签: ios objective-c uiviewcontroller