aswin
aswin

Reputation: 165

UIWebView Size Incorrect When Rotating

I have a UIWebView that only cover majority of the right part of the screen. To do this, I specify the frame size and position for portrait orientation using the frame property. webView.frame=CGRectMake(220,100,548,875);

When the device is rotated to landscape, I specify a new frame for landscape orientation: webView.frame=CGRectMake(300,73,724,638);

The problem is when I rotate the webView from portrait to landscape and to portrait again and i changed the frame to the original one, its width is only a half from before..

Here are my codes and function called to resize the frames:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
    }
    else if(self.interfaceOrientation==UIInterfaceOrientationPortrait)
    {
        [self showLandscape];
    }
    else if(toInterfaceOrientation==UIInterfaceOrientationPortrait)
    {
        [self showPortrait];
    }
}

-(void)showLandscape
{
.
.
.
webView.frame=CGRectMake(300,73,724,638);
.
.
.
}

-(void)showPortrait
{
.
.
.
webView.frame=CGRectMake(220,100,548,875);
.
.
.
}

How to solve this problem?

Upvotes: 0

Views: 914

Answers (1)

holex
holex

Reputation: 24041

this is what I've done, it is working well:

- (void)viewDidLoad {
    UIWebView *_webView = [[UIWebView alloc] init];
    // when I add these line it is also working well (!!!) on my device
    //_webView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
    //_webView.autoresizesSubviews=YES;
    //_webView.contentMode=UIViewContentModeScaleAspectFit; 
    [self.view addSubview:_webView];
    [_webView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)];
}

and I've added the following code as well:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    for (UIView *_temporaryView in self.view.subviews) {
        if ([_temporaryView isKindOfClass:[UIWebView class]]) {
            if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
                [_temporaryView setFrame:CGRectMake(220.f, 100.f, 548.f, 875.f)];
            } else {
                [_temporaryView setFrame:CGRectMake(300.f, 73.f, 724.f, 638.f)];
            }
        }
    }

    return true;
}

please, compare it with your code and find the differences, if you don't want to provide you code fragment to find the anomaly.

Upvotes: 1

Related Questions