UIWebView:使框架适合视图大小,允许在 UISplitViewController 上的该框架内滚动

发布于 2024-12-11 15:56:03 字数 1435 浏览 0 评论 0原文

这个问题快把我逼疯了。

我有一个 UIWebView,我试图填充 UISplitViewController 右侧的完整视图:

CGSize boundsSize = self.view.bounds.size;
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, boundsSize.width, boundsSize.height)] autorelease];
self.webView.delegate = self;
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor redColor];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:webView];

首先,当我不​​知道方向时,如何使框架大小适合视图?我可以自动调整到合适的大小吗?

接下来,当我将 HTML 加载到 webView 中时,我希望框架大小保持不变(就像 UIScrollView 一样),但让新内容能够随该框架滚动。有些内容较长,有些则不然。所以,我尝试调整框架,但这只会使我的框架更大,而不是我的内容大小。

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {

    // Calculate the size of our webview
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

}//end

这不可能吗?我是否需要将 webView 放入 UIScrollView 中,并在每次将其加载到 webView 中时将其 contentSize 设置为新内容的高度?

This problem is driving me nuts.

I have a UIWebView which I am trying to make fill the full view on the right side of a UISplitViewController:

CGSize boundsSize = self.view.bounds.size;
self.webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, boundsSize.width, boundsSize.height)] autorelease];
self.webView.delegate = self;
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor redColor];
self.webView.allowsInlineMediaPlayback = YES;
self.webView.dataDetectorTypes = UIDataDetectorTypeAddress | UIDataDetectorTypeLink | UIDataDetectorTypePhoneNumber;
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[self.view addSubview:webView];

First of all, how do I account for making the frame size fit the view when I don't know the orientation? Can I autoresize to the appropriate size?

Next, when I load my HTML into my webView I want the frame size to stay the same (just like a UIScrollView does), but have that new content be able to be scrolled with that frame. Some content is much longer, some is not. So, I have tried to adjust the frame, but this just makes my frame larger rather than my content size.

- (void)webViewDidFinishLoad:(UIWebView *)aWebView {

    // Calculate the size of our webview
    CGRect frame = aWebView.frame;
    frame.size.height = 1;
    aWebView.frame = frame;
    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    aWebView.frame = frame;

}//end

Is this not possible? Do I need to put the webView inside of a UIScrollView and set the contentSize of that to the height of my new content every time I load it into my webView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋意浓 2024-12-18 15:56:04

我认为这就是让这项工作成功所需的全部。

[articleWebView sizeThatFits:CGSizeZero];

I think that is all you need to make this work.

[articleWebView sizeThatFits:CGSizeZero];

野稚 2024-12-18 15:56:04

似乎我必须在加载时手动调整框架:

    // Orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    // Create frame
    CGRect frame;
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        frame = CGRectMake(0, 0, 748, 1024);
    } else {
        frame = CGRectMake(0, 0, 768, 1022);
    }//end

Seems I had to manually adjust my frame on load:

    // Orientation
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];

    // Create frame
    CGRect frame;
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        frame = CGRectMake(0, 0, 748, 1024);
    } else {
        frame = CGRectMake(0, 0, 768, 1022);
    }//end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文