OGeek|极客世界-中国程序员成长平台

标题: iOS:调整 UIWebView 的高度 - 奇怪的行为 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:38
标题: iOS:调整 UIWebView 的高度 - 奇怪的行为

我有一个问题。在 Xcode 我有一个 ViewController 嵌入 View > ScrollView 并且 Scrollview 内部是一个 UIWebView。

应用程序读取一些 XML 数据并将格式化的文本粘贴到 UIWebView 中。 UIWebView-Element之后是另一个Element,所以我要动态调整UIWebView的高度。

加载 View 后一切正常,但是当我点击屏幕时, View 将 scrollview 重新调整为 Storyboard高度。

为什么?

- (void)webViewDidFinishLoadUIWebView *)aWebView
{
aWebView.scrollView.scrollEnabled = NO;    // Property available in iOS 5.0 and later
CGRect frame = aWebView.frame;

frame.size.width = 280;       // Your desired width here.
frame.size.height = 1;        // Set the height to a small one.

aWebView.frame = frame;       // Set webView's Frame, forcing the Layout of its embedded     scrollView with current Frame's constraints (Width set above).

frame.size.height = aWebView.scrollView.contentSize.height;  // Get the corresponding         height from the webView's embedded scrollView.

aWebView.frame = frame;       // Set the scrollView contentHeight back to the frame itself.

NSLog(@"size: %f, %f", aWebView.frame.size.width, aWebView.frame.size.height);

// Position URL Label
CGRect frame_label = fachmesseDetailWebsite.frame;
NSLog(@"x: %f", frame_label.origin.x);
NSLog(@"y: %f", frame_label.origin.y);
frame_label.origin.y=aWebView.frame.size.height+115;//pass the cordinate which you want
frame_label.origin.x= 20;//pass the cordinate which you want
fachmesseDetailWebsite.frame= frame_label;
NSLog(@"x: %f", frame_label.origin.x);
NSLog(@"y: %f", frame_label.origin.y);

}



Best Answer-推荐答案


每当您更改框架时,您必须重新加载 webview,然后只有 webview 框架会更改您放置的任何内容,否则它不会影响 webview。

[webview reload];

编辑: 这个工作

UIWebView *web=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    NSString* html = [NSString stringWithFormat"<html><head><style>a { color: #db5226; text-decoration: none; font-weight: bold; } body { padding: 0; margin: 0; font-size:45px;font-family: Helvetica;text-align:left; color:black; } </style></head<body>%@</body></html>",@"helow"];
    [web loadHTMLString:html baseURL:nil];
    [self.view addSubview:web];

关于iOS:调整 UIWebView 的高度 - 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16185258/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4