更改方向时定位UIview

dai*_*dai 3 iphone objective-c uiviewcontroller uiview uiinterfaceorientation

videoView在InterfaceBuilder的UIViewController中有一个自定义的UIView .

当我加载它,它看起来很好,但当我把它说成"从肖像景观"它不会去坐标告诉它的地方.然后,当我把它转回来时,它又错了.

我有以下代码:

-(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];
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 5

视图旋转时,请确保您从纵向移动到横向:

if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)
Run Code Online (Sandbox Code Playgroud)

因为方向尚未改变,而不是

videoView.frame = CGRectMake(20.0f, 77.0f, 984.0f, 595.0f);
Run Code Online (Sandbox Code Playgroud)

写:

videoView.frame = CGRectMake(77.0f, 22.0f, 595.0f, 984.0f);
Run Code Online (Sandbox Code Playgroud)

或者,您可以进入Interface Builder并在那里更改自动调整遮罩,并且不会在代码中执行任何操作.要更改自动调整遮罩,请转到右窗格中的"大小"检查器(标尺图标).你会看到一个内部有一个垂直和水平箭头的正方形,并且每边都有一个字母I形状的线条.当红色(打开,单击以打开)时,内部的箭头将使视图调整大小时对象在该方向上自动伸展.打开I形线基本上将视图停靠到那一侧,这意味着视图侧面与该侧面之间的距离将始终保持不变.例如,如果顶部的工具栏在横向上变薄,请不要选择顶行,否则您的视图将离顶部工具栏最远几英里.

或者您可以通过调用返回no进行自动旋转并自行实现

self.interfaceOrientation = toInterfaceOrientation;
Run Code Online (Sandbox Code Playgroud)

然后手动更改原始代码中的坐标.

你真的需要试验它,我一直遇到这个问题,每次都用不同的方法修复它.