0

I have the following code

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

I am creating frame with CGRect and I am using it display my UI elements like labels, buttons etc. I am calculating position based on the window size so that they are in appropriate positions in different orientations. When my app is running, I click on home button. When I open the app again, my UI elements are messed up. They are not in proper positions. The method I mentioned above gets invoked every time I change the orientation and open the app. So, this is getting invoked when I reopen the app. But the problem is that, even before the frame is completely formed, it is taking the width and height at that particular point and calculating positions of my UI elements. This is leading to messed up UI. Is there any way where in I can restrict it to take width and height only after the frame is completely formed? Thanks!

ldiqual
  • 15,015
  • 6
  • 52
  • 90
user1982519
  • 515
  • 5
  • 24
  • When are you calling that method? You are probably calling it too soon, before the view's frame exists. Try to do it in ViewDidLoad. – Odrakir Feb 22 '13 at 19:12
  • I am implementing UIView so I cannot have viewDidLoad method. I subscribed for notifications when the device orientation changes and when application becomes active from inactive state. I am invoking it there. To be more precise, I am invoking it as selector for both UIApplicationDidBecomeActiveNotification and UIDeviceOrientationDidChangeNotification – user1982519 Feb 22 '13 at 19:17
  • btw, the view's frame exist because I am able to see my UI elements..but I guess the frame is not completely formed because of which it is taking wrong width and height and displacing it in undesired way. – user1982519 Feb 22 '13 at 19:18
  • http://stackoverflow.com/questions/5677716/how-to-get-the-screen-width-and-height-in-ios I referred to this thread and got my problem solved. Thanks! – user1982519 Feb 22 '13 at 19:40

0 Answers0