【问题标题】:Position UIview when changing orientation改变方向时定位 UIview
【发布时间】:2011-07-08 08:02:04
【问题描述】:

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

【问题讨论】:

标签: iphone objective-c uiview uiviewcontroller uiinterfaceorientation


【解决方案1】:

当视图旋转时,请确保您从纵向变为横向:

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;

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

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

【讨论】:

  • 嘿,您还可以提供有关自动调整大小蒙版的更多信息以供参考。 +1
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多