我正在从网上加载图像,并将其下的一些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];
并且正如其他人所说的,您需要设置内容大小 – mrosales
我本打算抓取图像并在应用加载时缓存它,但我正在做这种方式最初只是因为它可以快速轻松地完成布局。这就是说,如果我不打算使用webview有没有更好的方法来呈现和HTML字符串?文本内容来自联机管理的cms,将文本保存为用于格式化的html。 – LoneWolfPR
我开始认为我的解决方案是一起摆脱scrollview。将所有东西用作webview。然后我可以缓存图像和文本,并将它们一起加载到webview中。 – LoneWolfPR