0

我有以下代码

- (void) setTargetGoalFrameToLeftOfWindow: (UIView*) goalView orientation: (UIInterfaceOrientation) orientation {
    [goalView setTransform:CGAffineTransformMakeRotation(-M_PI_2)];

    CGRect goalFrame = goalView.frame;
    CGRect windowFrame = [self getWindowFrame];

    CGFloat topMargin;
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        topMargin = (windowFrame.size.width - goalFrame.size.height) / 2.0;
    } else {
        topMargin = (windowFrame.size.height - goalFrame.size.height) / 2.0;
    }

    goalView.frame = CGRectMake(GOAL_MARGIN, topMargin, goalFrame.size.width,     goalFrame.size.height);
}

我正在使用 CGRect 创建框架,我正在使用它来显示我的 UI 元素,如标签、按钮等。我正在根据窗口大小计算位置,以便它们在不同方向处于适当的位置。当我的应用程序运行时,我单击主页按钮。当我再次打开应用程序时,我的 UI 元素被弄乱了。他们不在适当的位置。每次更改方向并打开应用程序时,都会调用我上面提到的方法。所以,当我重新打开应用程序时,它会被调用。但问题是,即使在框架完全形成之前,它也会获取该特定点的宽度和高度并计算我的 UI 元素的位置。这导致混乱的用户界面。有没有什么办法可以限制它只有在框架完全形成后才采用宽度和高度?谢谢!

4

0 回答 0