Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebview full screen size

I am trying to display a pdf in a UIWebview using the entire screen of the device, except the status bar and top bar. So far, I created IBOutlet UIWebview *webview; in my .h file. I connected the webview in my storyboard and wrote the following code in my .m file:

- (void)viewDidLoad
{
    [super viewDidLoad];

    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];

    webview.scalesPageToFit = YES;
    webview.autoresizesSubviews = YES;
    webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webview setBackgroundColor:[UIColor clearColor]];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Chart" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
    [webview loadRequest:request];

    [self.view addSubview:webview];

}

The pdf displays correctly in my 3.5 inch, but in my 4 inch display, the bottom is cut off. This is even more noticeable when I rotate to landscape view. About 33% of the screen is not used at all. How can I fix this? I know it must have something to do with the UIWebview dimensions in my code above (webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];), but is there a better way to simply display the webview on the entire screen, without specifying a specific screen height and width?

like image 999
George Friday Avatar asked Aug 31 '13 20:08

George Friday


3 Answers

One cheap thing to do would be to set the frame of the webview in overriding - (void)viewDidLayoutSubviews

- (void)viewDidLayoutSubviews {
    webView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

the advantage of doing it here, is that the size and orientation of the view has already been determined here, and you can get an accurate measurement.

like image 190
HalR Avatar answered Nov 15 '22 08:11

HalR


Using Auto Layout

Be sure the "Constrain to margins" option is not checked, all constrain values are 0:

enter image description here

And your webview is a first child of your main view:

enter image description here

Without Auto Layout

Change webview autosizing connections:

enter image description here

like image 32
dianakarenms Avatar answered Nov 15 '22 09:11

dianakarenms


After a lot of headache dealing with compatibility of iOS6, 7 and different screen sizes, I use this one:

- (void)viewDidLoad {
   [super viewDidLoad];

   self.webview = [[UIWebView alloc] init];
   // More setting your webview here

   // Set webview to full screen
   self.view = self.webview;

   [self.webview loadRequest:aRequest]; 
}
like image 2
giacatho Avatar answered Nov 15 '22 10:11

giacatho