UIWebview full screen size

2019-04-05 18:53发布

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?

4条回答
姐就是有狂的资本
2楼-- · 2019-04-05 19:30

I was able to get this working correctly for me by selecting the Reset to Suggested Constraints under the Resolve Auto Layout Issues menu. enter image description here

查看更多
神经病院院长
3楼-- · 2019-04-05 19:31

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

查看更多
三岁会撩人
4楼-- · 2019-04-05 19:34

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]; 
}
查看更多
戒情不戒烟
5楼-- · 2019-04-05 19:44

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.

查看更多
登录 后发表回答