我正在从网络上加载一个图像,并将它下面的一些 html 加载到 UIScrollView 中。它位于 tabbarcontroller 内的 navigationController 内。当我加载子视图时,滚动视图不会滚动。我错过了什么?
这是 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];
}
更新
我添加了以下几行,但仍然没有任何变化。我根本无法滚动。
[sv setContentSize:CGSizeMake(320.0, 750.0)];
[[wv scrollView] setScrollEnabled:NO];
[[wv scrollView] setBounces:NO];
[wv setUserInteractionEnabled:NO];