2013-06-21 57 views
0

我正在从网上加载图像,并将其下的一些html加载到UIScrollView中。这位于tabbarcontroller内的navigationController内。当我加载子视图时,滚动视图不会滚动。我错过了什么?添加子视图后,iOS:ScrollView不滚动

这里的viewDidLoad中:

- (void)viewDidLoad 
{ 
NSURL *imageURL = [NSURL URLWithString:[[self exhibit] image]]; 

NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
UIImage *image = [UIImage imageWithData:imageData]; 

if ([[UIScreen mainScreen] bounds].size.width < 600) { 
    CGSize newSize = CGSizeMake(300.0, 140.6); 
    UIGraphicsBeginImageContext(newSize); 
    [image drawInRect:CGRectMake(0.0, 0.0, newSize.width, newSize.height)]; 
    image = UIGraphicsGetImageFromCurrentImageContext(); 
} 

CGRect viewFrame = CGRectMake(0.0, 0.0, [[UIScreen mainScreen] bounds].size.width, 
           ([[UIScreen mainScreen] bounds].size.height - [[self navigationController] navigationBar].bounds.size.height - [[[self tabBarController] tabBar] bounds].size.height)); 
NSLog(@"%@",NSStringFromCGRect(viewFrame)); 

UIScrollView *sv = [[UIScrollView alloc] initWithFrame:viewFrame]; 
[sv setScrollEnabled:YES]; 
[self setView:sv]; 

CGRect frame = CGRectMake(0.0, 0.0,image.size.width,image.size.height); 
UIImageView *iv = [[UIImageView alloc] initWithFrame:frame]; 
[iv setImage:image]; 
[iv setContentMode:UIViewContentModeScaleAspectFit]; 
[iv setTranslatesAutoresizingMaskIntoConstraints:NO]; 
[iv setBackgroundColor:[UIColor redColor]]; 
[sv addSubview:iv]; 
[self setImageView:iv]; 

NSDictionary *nameMap = @{@"imageView" : [self imageView]}; 

NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-10-[imageView]-10-|" 
                     options:NSLayoutFormatAlignAllCenterX 
                     metrics:nil 
                      views:nameMap]; 
[sv addConstraints:horizontalConstraints]; 

NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[imageView]" 
                     options:NSLayoutFormatAlignAllTop 
                     metrics:nil 
                      views:nameMap]; 
[sv addConstraints:verticalConstraints]; 

UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 150.0, 300.0, 600.0)]; 
[wv loadHTMLString:[[self exhibit] text] baseURL:nil]; 
[sv addSubview:wv]; 
[self setExhibitText:wv]; 


} 

UPDATE

我已经添加了以下行,而且也仍然没有改变。我无法滚动。

[sv setContentSize:CGSizeMake(320.0, 750.0)]; 
[[wv scrollView] setScrollEnabled:NO]; 
[[wv scrollView] setBounces:NO]; 
[wv setUserInteractionEnabled:NO]; 

回答

0

您不会出现在您的滚动视图中设置一个contentSize - 没有内容大小,滚动视图将不知道你想多远滚动。默认情况下,UIScrollView具有零值内容大小,因此需要设置。

0

ScrollView需要contenSize和框架设置为非零大小。默认情况下:

scrollView.contentSize = CGRectZero; 

在你的情况下,内容大小应该是CGRect等同于imageView和webView的组合在一起。

0

有几件事情,你应该修复你的代码。首先,您正在主线程上同步下载来自互联网的图像。您不应该在主线程上从互联网加载数据。

你遇到的另一个问题是UIWebView并不是真的打算嵌入在UIScrollView中。由于UIWebView具有自己的滚动视图,因此webview将阻止对UIScrollView的所有接触。如果网页视图有静态内容,您可以做webview.setUserInteractionEnabled = NO

+0

并且正如其他人所说的,您需要设置内容大小 – mrosales

+0

我本打算抓取图像并在应用加载时缓存它,但我正在做这种方式最初只是因为它可以快速轻松地完成布局。这就是说,如果我不打算使用webview有没有更好的方法来呈现和HTML字符串?文本内容来自联机管理的cms,将文本保存为用于格式化的html。 – LoneWolfPR

+0

我开始认为我的解决方案是一起摆脱scrollview。将所有东西用作webview。然后我可以缓存图像和文本,并将它们一起加载到webview中。 – LoneWolfPR