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

时间:2013-04-24 06:52:53

标签: ios xcode uiwebview scrollview

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

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

加载视图后一切正常,但是当我点击屏幕时,视图会将scrollview重新调整为其故事板高度。

为什么?

- (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);

}

1 个答案:

答案 0 :(得分:1)

每当你改变框架时,你必须重新加载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];