Iphone 更改方向时放置UIview

Iphone 更改方向时放置UIview,iphone,objective-c,uiview,uiviewcontroller,uiinterfaceorientation,Iphone,Objective C,Uiview,Uiviewcontroller,Uiinterfaceorientation,我在InterfaceBuilder的UIViewController中有一个自定义UIViewvideoView 当我加载它时,它看起来很好,但当我把它转到“从肖像到风景”时,它不会转到坐标指示的地方。然后当我把它转回去的时候,它又错了 我有以下代码: -(void)viewWillAppear:(BOOL)animated { [self processRotation:self.interfaceOrientation]; } - (BOOL)shouldAutorotateTo

我在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];
}

您正在使用
willRotateToInterfaceOrientation:
,它在旋转发生之前被调用。如果videoView设置了自动调整大小的任务,那么自动调整大小将在稍后启动并更改视图的位置


您通常应该在
-(void)WillAnimateRotationInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration中执行此操作,并确保当视图旋转时,您的视频视图的autoresizingMask设置为
UIViewAutoResizingOne
,确保从纵向到横向:

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形线基本上会将视图停靠到该侧,这意味着视图侧和该侧之间的距离将始终保持不变。 e、 g.如果顶部有一个工具栏,但在横向中变薄了,请不要选择顶行,否则您的视图将与顶部工具栏相距数英里

或者,您可以为自动旋转返回no,并通过调用

self.interfaceOrientation = toInterfaceOrientation;
然后手动更改坐标,如原始代码中所示


你真的需要尝试一下,我一直都遇到这个问题,每次都会以不同的方式解决。

嗨,黛黛黛,我可以推荐我最近的一篇帖子来帮助你回答问题:嘿,你还可以提供更多关于自动调整大小蒙版的信息供参考+1.