ios UIWebview 全屏尺寸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18552416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:55:50  来源:igfitidea点击:

UIWebview full screen size

iosxcodeuiwebview

提问by George Friday

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:

我正在尝试使用设备的整个屏幕在 UIWebview 中显示 pdf,状态栏和顶部栏除外。到目前为止,我是IBOutlet UIWebview *webview;在我的 .h 文件中创建的。我在我的故事板中连接了 webview 并在我的 .m 文件中编写了以下代码:

- (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?

pdf 在我的 3.5 英寸显示器中正确显示,但在我的 4 英寸显示器中,底部被切断。当我旋转到横向视图时,这一点更加明显。大约 33% 的屏幕根本没有使用。我怎样才能解决这个问题?我知道它一定与我上面代码中的 UIWebview 尺寸有关 ( webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];),但是有没有更好的方法可以在整个屏幕上简单地显示 webview,而不指定特定的屏幕高度和宽度?

回答by HalR

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

一件便宜的事情是在覆盖中设置 webview 的框架 - (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.

在这里做的好处是,这里已经确定了视图的大小和方向,你可以得到一个准确的测量结果。

回答by dianakarenms

Using Auto Layout

使用自动布局

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

确保未选中“约束到边距”选项,所有约束值为 0:

enter image description here

在此处输入图片说明

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

你的 webview 是你的主视图的第一个孩子:

enter image description here

在此处输入图片说明

Without Auto Layout

没有自动布局

Change webview autosizing connections:

更改 webview 自动调整连接:

enter image description here

在此处输入图片说明

回答by giacatho

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

在处理 iOS6、7 和不同屏幕尺寸的兼容性问题后,我使用了这个:

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

回答by Joe Saunders

I was able to get this working correctly for me by selecting the Reset to Suggested Constraintsunder the Resolve Auto Layout Issuesmenu. enter image description here

通过选择菜单Reset to Suggested Constraints下的,我能够使其正常工作Resolve Auto Layout Issues在此处输入图片说明