UIWebview full screen size

ぃ、小莉子 提交于 2019-12-05 19:16:06

问题


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?


回答1:


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.




回答2:


Using Auto Layout

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

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

Without Auto Layout

Change webview autosizing connections:




回答3:


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]; 
}



回答4:


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



来源:https://stackoverflow.com/questions/18552416/uiwebview-full-screen-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!