我正在尝试在滚动视图中将图像放在图像下方。此代码导致空白屏幕。我确信它非常简单(我是初学者),但是有人能指出我的错误吗?
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = [self.detailItem title];
//self.webView = [[UIWebView alloc] init];
//self.imageView = [[UIImageView alloc] init];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.detailItem imageURL]]]];
self.imageView.image = image;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[self.detailItem url]]]];
[self.scrollView addSubview:self.imageView];
[self.scrollView addSubview:self.webView];
self.imageView.frame = CGRectMake(0, 770, 320, 230);
[self.webView setFrame:CGRectMake(0, 0, 320, 770)];
CGFloat scrollViewHeight = 1000.0f;
/*
for (UIView* view in scrollView.subviews)
{
NSLog(@"%f",view.frame.size.height);
scrollViewHeight += view.frame.size.height;
}
*/
[scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
NSLog(@"%f", self.imageView.frame.size.height);
NSLog(@"%f", self.webView.frame.size.height);
[self configureView];
}
来自.h
@interface DetailViewController : UIViewController <UIScrollViewDelegate>{
UIScrollView* scrollView;
}
@property (nonatomic, retain) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) id detailItem;
@property (nonatomic, strong) UIWebView *webView;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (nonatomic, strong) UIImageView *imageView;
@end
答案 0 :(得分:0)
在给定的代码中,您不初始化self.scrollView。
答案 1 :(得分:0)
您通过重新初始化这些值而丢失了对您的ivar的原始引用,并且您没有设置框架,因此它们的高度值为0,这就是为什么您的滚动视图没有变大:
self.webView = [[UIWebView alloc] init];
self.imageView = [[UIImageView alloc] init];
尝试将它们更改为仅在值为nil时初始化
if(self.webView == nil){
self.webView = [[UIWebView alloc] init];
}
if(self.imageView == nil){
self.imageView = [[UIImageView alloc] init];
}
//set frames for subviews
UIImage *remoteLoadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.detailItem imageURL]]];
[[self.imageView setFrame:CGRectMake(0,0,remoteLoadedImage.size.width, remoteLoadedImage.size.height)];
[self.imageView.image = remoteLoadedImage];
//i'm setting an arbitrary height and width
[self.webView setFrame:CGRectMake(0,self.imageView.frame.size.height+5, 320, 450)];