2011-03-29 89 views
1

這是對早期問題的更新。一旦我用一些基於字符串的HTML內容加載一個UIWebView,有沒有辦法確定視圖是否需要滾動來查看整個內容?我正在尋找某種標誌或方式來了解內容是否低於UIWebView的底部。確定UIWebView是否需要滾動

提前致謝!

回答

1

你可以繼承的UIWebView並使用以下initalizers:

-(id) initWithCoder:(NSCoder *)aDecoder 
{ 
    if(self = [super initWithCoder:aDecoder]) 
    { 
     for (UIView* v in self.subviews){ 
      if ([v isKindOfClass:[UIScrollView class]]){ 
       self.scrollview = (UIScrollView*)v; 
       break; 
      } 
     } 
    } 
    return self; 
} 

- (id) initWithFrame:(CGRect)frame{ 
    if(self = [super initWithFrame:frame]) 
    { 
     for (UIView* v in self.subviews){ 
      if ([v isKindOfClass:[UIScrollView class]]){ 
       self.scrollview = (UIScrollView*)v; 
       break; 
      } 
     } 
    } 
    return self; 
} 

然後有一個屬性叫做

@property (nonatomic, retain) UIScrollView *scrollview; 

然後,您可以訪問的UIWebView中的滾動視圖,可以檢查其內容的大小看看它是否大於你的意見大小

+0

這是一個有點工作,好主意。 我做了這樣的檢查:(w是我的subclassed webview) [w loadHTMLString:content baseURL:url]; \t \t \t [scrollIssue addSubview:w]; \t \t \t \t \t \t \t \t \t的CGRect RECT = w.frame; \t \t \t UIScrollView * sv = [w scrollview]; \t \t \t CGSize rectv = sv.contentSize; 即使需要滾動,它們也總是一樣的。懷疑它沒有被繪製,有沒有辦法在檢查之前強制它佈局? – 2011-03-29 20:47:06

+0

可以嘗試調用webview上的layoutSubviews? – JFoulkes 2011-03-30 08:17:17

0

最簡單的方法可能是使用Javascript來獲取文檔高度:

NSInteger height = [[myWebView stringByEvaluatingJavaScriptFromString:@「document.body.scrollHeight」] intValue];

(沒有實際測試,所以因人而異,不同的瀏覽器把文檔高度不同的對象和屬性,我不記得在Webkit的其中一個作品......看到How to get height of entire document with JavaScript?

如果高度返回大於它需要滾動的UIWebView高度。