2013-05-02 50 views
0

假设我有一个的堆积对一个UIImageView一个UIWebView(或其他视图):调整大小的UIWebView上webViewDidFinishDownload困境

enter image description here

我想调整我的UIWebView,让我知道放在哪里的UIImageView 。我这样做的方式是等待的UIWebView完成加载:

webViewCalculating = YES; 
while (webViewCalculating == YES) { 
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 
} 

然后我调整它当一个UIWebView加载完成后,根据this suggestion

-(void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
    CGRect frame = webView.frame; 
    frame.size.height = 1; 
    webView.frame = frame; 

    CGSize fittingSize = [webView sizeThatFits:CGSizeZero]; 
    frame.size = fittingSize; 
    webView.frame = frame; 

    heightSoFar += webView.frame.size.height; 
    webViewCalculating = NO; 
} 

这一点后,我d知道我的UIWebView有多高,并且我可以相应地放置我的UIImageView。然而,当我推动另一个视图控制器,并回到这个,我的哈克[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode似乎并没有等到我的webView完成计算高度第一。有没有更好的方式让我知道我的UIWebView的大小,而无需等待它加载?也许类似于NSString的sizeWithFont方法?

+0

为什么你使用while循环呢?一旦webview完成加载,你可以在那里调整大小吗? – 2013-05-02 14:17:49

+0

我需要等待看它有多大,否则图像视图将重叠。 – 2013-05-03 00:55:29

+0

我不知道我得到这个。难道你只是保持图像隐藏,直到'didFinishLoad'委托方法被调用,然后计算它的框架并淡入它? – Rog 2013-05-03 02:34:18

回答

4

你不应该在做你的while循环,这是抽运行循环。这是一个非常糟糕的主意。你应该让你的滚动视图容器响应,并在webview完成加载时重新布局子视图。从“合理大小的网页浏览区域”开始。也许在网页内容加载时显示一个微调。

的技术,我以前做的正是这种如下:

首先一些类方法添加到一个UIWebView如下:

@interface UIWebView (ContentSize) 
- (CGFloat)documentOffsetHeight; 
@end 

@implementation UIWebView (ContentSize) 
- (CGFloat)documentOffsetHeight 
{ 
    return [[self stringByEvaluatingJavaScriptFromString:@"document.documentElement.offsetHeight"] floatValue]; 
} 
@end 

然后我写了一个包含一个UIView子类UIWebView的。例如:

@interface MyWebView : UIView <UIWebViewDelegate> 
@end 

@implementation MyWebView 
{ 
    BOOL _loaded; 
    UYIWebView *_webView; 
} 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) 
    { 
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 1)]; 
    _webView.delegate = self; 
    _webView.alpha = 0.0f; 
    [self addSubview:_webView] 
    } 
} 

- (CGSize)sizeThatFits:(__unused CGSize)size 
{ 
    if (_loaded) 
    { 
    CGFloat height = [webView_ documentOffsetHeight]; 
    CGFloat width = self.frame.size.width; 
    return CGSizeMake(width, height); 
    } 

    return self.frame.size; 
} 

- (void)sizeToFit 
{ 
    CGSize fittingSize = [self sizeThatFits:CGSizeZero]; 
    self.bounds = CGRectMake(0, 0, fittingSize.width, fittingSize.height); 
} 

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 
    _webView.frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height); 
} 

- (void)loadHTMLString:(NSString *)htmlString baseURL:(NSURL *)baseURL 
{  
    // This code assumes jQuery is already used in the HTML content. If not, added it as a script resource here too. 
    NSString *scriptTag = @"<script type=\"text/javascript\" >jQuery(document).ready(function() { window.location = 'x-webview://ready'; });</script>\n"; 

    NSString *const headOpeningTag = @"<head>"; 
    if ([htmlString rangeOfString:headOpeningTag].location != NSNotFound) 
    { 
    htmlString = [htmlString stringByReplacingOccurrencesOfString:headOpeningTag 
                 withString:[headOpeningTag stringByAppendingString:headContent] 
                  options:NSCaseInsensitiveSearch 
                  range:NSMakeRange(0, [htmlString length])]; 
    } 
    else 
    { 
    htmlString = [headContent stringByAppendingString:htmlString]; 
    } 

    [_webView loadHTMLString:htmlString baseURL:baseURL]; 
} 

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if ([request.URL.scheme isEqualToString:@"x-webview"] && [request.URL.host isEqualToString:@"ready"]) 
    { 
    _loaded = YES; 

    [UIView animateWithDuration:0.1 
          delay:0 
         options:UIViewAnimationOptionAllowUserInteraction 
        animations:^{ webView.alpha = 1.0f; } 
        completion:nil]; 

    [self sizeToFit]; 
    return NO; 
    } 

    return YES; 
} 

@end 

所以基本上这个UIView子类所做的就是嵌入一个不可见的_webview。然后,当文档加载并呈现基于jQuery的JavaScript尝试使用自定义URL方案从当前页面导航离开时。此导航被困于拒绝。但是,为了适应这些视图大小并从HTML文档中获取适当的大小。

请注意,您不必使用jQuery。您可以添加DOM JavaScript事件。我只是更熟悉jQuery如何处理原始DOM事件。

在你的情况下,你将不得不与沟通的scrollview内容已完成加载和滚动视图视图应重新布局。你可以使用委托协议或类似的方法来做到这一点。或者也许scrollview是“MyWebView”容器UIView子类。根据需要调整。

+0

非常感谢,我最后只是使用你的javascript方法,并在最后一次webViewDidFinishLoad调用之后中继所有子视图。在UIWebView中没有像sizeWithFont方法那样的魔术,事实证明。 – 2013-05-04 01:24:55

-1

属性没有重置,所以当你回到这个控制器时,你需要webViewCalculating = YES; ,这样它就不会立即失败while循环。

如果你已经这样做,你可以尝试把

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; 

与在

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

} 

委托方法

希望这是非常有用的。

+0

我确实重置了webViewCalculating属性。把NSRunLoop放到那个委托方法中是无限循环的。 – 2013-05-03 01:10:19