0
votes

I added a webview to a UIViewController. I assumed that I could match it's size to the UIViewController's view, but soon realised that the UIControllerView frame was independent of the screen orientation, and so displayed the frame as it would be in portrait mode (so the wrong way around, if the screen was in landscape). Doing UIInterfaceOrientationIsPortrait() doesn't work, because if the device is lying flat then it still reports it as being in portrait, even if the screen is rotated to landscape.

To get around this problem, i checked the orientation of the status bar and then used the UIScreen bounds (also independent of rotation) to work out the current frame, and then removed the size of the status bar, if it's visible.

CGRect frame = [UIScreen mainScreen].bounds;

BOOL isPortrait;
UIDeviceOrientation orientation = (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {
        isPortrait = YES;
    }
    else {
        isPortrait = NO;
    }

    if (!isPortrait) {
        frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.height, frame.size.width);
    }

if ([UIApplication sharedApplication].statusBarHidden) {
        return frame;
    }
    else {
        return CGRectMake(frame.origin.x,
                          frame.origin.y,
                          frame.size.width,
                          frame.size.height - 20);
    }

Now I've run into another problem - even though I set up my UIWebView for autoresizing, the UIViewController's view's frame is independent of rotation, so it doesn't change, therefore my UIWebView doesn't change frame correctly upon rotation.

(Oddly, despite the UIViewController view frame not changing, the view does actually resize itself correctly)

I've tried using the method didRotateFromInterfaceOrientation:, which did resize it, but only after all the rotation animation was done.

Using willRotateToInterfaceOrientation:duration: poses the same problem as earlier - that UIInterfaceOrientationIsPortrait() isn't a true reflection of the orientation.

Anybody managed to do this?

1

1 Answers

2
votes

try autoresizing both webview and viewcontroller's view. or you can also take another view and delete default view in nib file of viewcontroller. Autoresize that view and connect to view outlet.