Search code examples
objective-ccocoaipad

Adding subviews programmatically on iPad, hard coding


I'm adding a custom status bar in my application to monitor upload progress. This works fine in portrait mode, but when I am in landscape and my custom status bar appears, it always appears on the opposite side of the home button. I think it's because I'm hard coding my frame.

I have the XIB file set to auto adjust its length.

take a look:

-(void) animateStatusBarIn {
    float x = window.frame.origin.x;
    float y = window.frame.origin.y;
    float height = 20;
    float width = window.frame.size.width;
    CGRect statusFrame = CGRectMake(x, y-20, width, height);
    iPadUploadStatusBar *statusView = [[iPadUploadStatusBar alloc] initWithNibName:@"iPadUploadStatusBar" bundle:nil];

    self.status = statusView;

    [statusView release];

    status.view.frame = statusFrame;
    [window addSubview:status.view];

    [UIView beginAnimations:@"slideDown" context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationFinished:)];
    statusWindow.frame = CGRectMake(0.0f, 0.0f, 1024.0f, 20.0f);    
    [UIView commitAnimations];
}

All is good in portrait orientation, but like I said, it always decides that the new status bar should go opposite of the home button.

By the way, this is inside AppDelegate.m, so I'm using the existing window that's in the default appDelegate file


Solution

  • Firstly, where are you calling your -animateStatusBarIn method from?

    You dont have to set the frame for iPadUploadStatusBar's view when rotation occurs (in case if you are calling the method animateStatusBar each time when orientation changes).

    All you have to do is, set the autoresizing masks for the view properly in your iPadUploadStatusBar nib and add it as a subview to window or a viewController's view which is already a subview of window. The rotation and its animation is all handled automatically for you.