1

我正在尝试将 web 视图放在滚动视图中的图像下方。此代码导致空白屏幕。我敢肯定这非常简单(我是初学者),但有人能指出我的错误吗?

- (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
4

2 回答 2

0

在给定的代码中,您没有初始化 self.scrollView。

于 2013-01-14T00:14:23.150 回答
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)];
于 2013-01-14T01:01:06.063 回答