Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a UIView completely when changing orientation?

I want to design a view/view controler that automaticaly shows/hides a subview when in landscape orientation. I want the subview to dissapear completely and other subviews to take up its space.

Using a UIViewController, I wrote code that sets the subviews' frame property and call it on:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;

This solves the problem most of the times but has problems when the orientation change is happening when the view is not appearing. to bypass this, I am also calling the resizing method on:

- (void)viewWillAppear:(BOOL)animated;

but this has problems in some rare cases (involving a UISearchDisplayController) so I am also calling the resizing method on

- (void)viewDidAppear:(BOOL)animated;

As you can understand, I am unhappy with this code and I am looking for a better/more performant way to do this.

Any ideas?

like image 274
Panagiotis Korros Avatar asked Mar 12 '10 14:03

Panagiotis Korros


1 Answers

Assuming all you have is a UIWebView and an ad banner, then you can just manually resize the webView when in landscape:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration
{
    if (toOrientation == UIInterfaceOrientationPortrait ||
        toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [adView setHidden:NO];
        }
    } else {
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [adView setHidden:YES];
        }       
    }
}

Then also do

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromOrientation 
                                duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;
    if (toOrientation == UIInterfaceOrientationPortrait ||
        toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [webView setBounds:CGRectMake(0.0,0.0,320.0,436.0)];
        }
    } else {
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [webView setBounds:CGRectMake(0.0,0.0,480.0,320.0)];
        }       
    }
}

The dimensions assume 44.0 height for the ad banner and no nav bar (44.0) or status bar (20.0), so you may need to adjust the numbers for your layout.

like image 88
PengOne Avatar answered Oct 22 '22 01:10

PengOne