我在InterfaceBuilder的UIViewController中有一个自定义UIView videoView
。
当我加载它时,它看起来很好,但当我把它转到"从纵向到横向"时,它不去坐标告诉它的地方。当我把它转回去时,它又错了。
我有以下代码:
-(void)viewWillAppear:(BOOL)animated
{
[self processRotation:self.interfaceOrientation];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void)processRotation:(UIInterfaceOrientation)_interfaceOrientation
{
if (_interfaceOrientation == UIInterfaceOrientationLandscapeLeft || _interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f);
}
else if (_interfaceOrientation == UIInterfaceOrientationPortrait || _interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
videoView.frame = CGRectMake(20.0f, 297.0f, 728.0f, 453.0f);
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[self processRotation:toInterfaceOrientation];
}
当视图旋转时,确保从纵向到横向:
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)
因为方向还没有改变,而不是
videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f);
写:
videoView.frame = CGRectMake(77.0f, 22.0f, 595.0f, 984.0f);
或者,您可以进入Interface Builder并在那里更改自动调整大小的蒙版,而不需要在代码中做任何操作。要更改自动调整大小的蒙版,请转到右侧窗格的大小检查器(标尺图标)。你会看到一个正方形,里面有一个垂直和水平的箭头,两边各有一条字母I形的线。内部的箭头,当为红色时(打开,单击打开),将使对象在视图调整大小时自动向该方向拉伸。打开一条I型线实际上是将视图停靠在另一边,这意味着视图的一边和另一边之间的距离将始终保持不变。例如,如果你的工具栏在顶部横向变薄,请而不是选择顶部线,否则你的视图将最终远离顶部工具栏。
或者您可以为自动旋转返回no并通过调用
自己实现它。self.interfaceOrientation = toInterfaceOrientation;
,然后像在原始代码中一样手动更改坐标。
你真的需要尝试一下,我总是遇到这个问题,每次我都以不同的方式解决它。
您正在使用willRotateToInterfaceOrientation:
,它在旋转发生之前被调用。如果videoView设置了它的autoresizingMask,那么autoresizing将在稍后启动并改变视图的位置。
您通常应该在- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
中这样做,并确保videoView的autoresizingMask设置为UIViewAutoresizingNone