5

I am having an issue with my UIWebView. When the view loads it loads in either orientation fine, fills the whole page perfectly etc.

However say if I load it in portrait then rotate the device the webview does not fill all the way across to the right hand side and I cannot for the life of me figure out why.

This is my view did load method

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib

    if  ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait){

        self.measurementsWebView.frame = CGRectMake(0, 0, 320, 367);

    }else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight){

        self.measurementsWebView.frame = CGRectMake(0, 0, 480, 218);
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"measurements" ofType:@"png"];
    [measurementsWebView loadHTMLString:[NSString stringWithFormat:@"<html><body><img src=\"file://%@\"></body></html>",path] baseURL:nil];
    measurementsWebView.scalesPageToFit = YES;

}

If i look in interface builder I am trying to find the panel that allows me to set expanding width or what ever you call it, but all I can see is this.

enter image description here

any help would be greatly appreciated

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
HurkNburkS
  • 5,492
  • 19
  • 100
  • 183

3 Answers3

4

Instead of setting the frame manually, you could also set the auto-resizing masks as:

measurementsWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

which will resize the webView in case you have no other elements on the screen (which, I take it, you don't have, from the values you've used when resizing measurementsWebView). Or, straight from the nib as shown in the following image:

auto-resize from nib

The masks can be set by clicking on the red bars in the box called Autosizing towards the bottom left.

Has AlTaiar
  • 4,052
  • 2
  • 36
  • 37
tipycalFlow
  • 7,594
  • 4
  • 34
  • 45
3

viewDidLoad is run once - when the view loads.

If you want to manually adjust the frame when rotation occurs, add code to to resize it in viewDidLayoutSubviews which is called whenever the view rotates.

-(void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    self.measurementsWebView.frame = self.view.bounds;

}
Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46
1

In interface builder, the drop-down menu where it says 'Layout Rectangle', click it and select 'Frame Rectangle'.

Alternatively you could override the willAnimateRotationToInterfaceOrientation: method in your view controller and set the co-ordinates/size manually as you have done in viewDidLoad.

sooper
  • 5,991
  • 6
  • 40
  • 65
  • weird, even when I do that its not changing the appearance of the menu... I think maybe I need to re-install xcode. I will try shouldAutototateToInterfaceOrientation now.. however will that change the size of the UIView Frame to fit the horizontal view. – HurkNburkS Jul 09 '12 at 01:00
  • Sorry, I meant to write `willAnimateRotationToInterfaceOrientation:` method. In a similar way to how you have checked for the orientation in your `viewDidLoad` method, you do the same here and adjust the view's frame. – sooper Jul 09 '12 at 01:21
  • For your problem with the interface builder, I recommend you read this post [here](http://stackoverflow.com/questions/9370072/xcode-4-3-not-presenting-autoresizing-panel-in-size-inspector) – sooper Jul 09 '12 at 01:27