iOS:调整 UIWebView 的高度 - 奇怪的行为

标签 ios xcode uiwebview scrollview

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

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

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

为什么?

- (void)webViewDidFinishLoad:(UIWebView *)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);

}

最佳答案

每当您更改框架时,您必须重新加载 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/

相关文章:

iphone - 请求苹果在iOS中使用功能?

android - 应用程序 : How to show the user first unread message rather than latest unread message?

ios - 将数据从一个 View 传递到另一个 View

xcode - 如何卸载Xcode 4.4命令行工具(Mac OS X 10.8 Mountain Lion)?

ios - 用于在页面上显示的 UIPageViewControlerDataSource 和 UiWebView

ios - UIImage 存储在 CoreData 到 UIWebView HTML Doc

ios - 如何查看UILabel当前的字体大小?

ios - 使用 CGMutablePath 创建路径会创建到错误 CGPoint 的行

iphone - 如何在 UITableViewCell 自动换行中制作文本?

ios - 为设备构建应用程序;想要终止进程然后能够重新启动它以便我可以在 Xcode 中看到 NSLog 消息