繁体   English   中英

Position UIview 改变方向时

[英]Position UIview when changing orientation

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

当我加载它时,它看起来很好,但是当我将它转为“从纵向横向”时,它并没有 go 坐标告诉它。 然后当我把它转回来时,它又错了。

我有以下代码:

-(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);

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

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

self.interfaceOrientation = toInterfaceOrientation;

然后像原始代码一样手动更改坐标。

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

您正在使用willRotateToInterfaceOrientation: ,它在旋转发生之前被调用。 如果 videoView 设置了它的 autoresizingMask,那么 autoresizing 将在稍后启动并更改视图的 position。

您通常应该在- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration中执行此操作并确保您的 videoView 的 autoresizingMask 设置为UIViewAutoresizingNone

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM