1

我正在尝试将 ViewControllerClass 的视图添加到 ScrollView。

问题是只有第一页显示,其他的都是空白的。

这是我的代码

for (int i = 0; i < 8; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    [imageArray2 addObject:[[WeatherViewController alloc] initWithNibName:@"WeatherViewController" picname:[NSString stringWithFormat:@"s%@.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:@"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]];
}

scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);

for(WeatherViewController *iv in imageArray2)
{
    [self.scrollView addSubview:iv.view];
}

有什么建议吗?

4

2 回答 2

2

您必须将您的计算框架分配给您WeatherViewController的循环视图。

尝试这个

for (int i = 0; i < 8; i++) {
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    WeatherViewController *myWeatherVC = [[WeatherViewController alloc] initWithNibName:@"WeatherViewController" picname:[NSString stringWithFormat:@"s%@.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:@"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
    myWeatherVC.view.frame = frame;


    [imageArray2 addObject:myWeatherVC];
    [self.scrollView addSubview:myWeatherVC.view];

}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
于 2013-10-18T08:38:40.147 回答
1

只需像这样分配框架,它就会起作用。

for (int i = 0; i < 8; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        WeatherViewController *thisView = [[WeatherViewController alloc] initWithNibName:@"WeatherViewController" picname:[NSString stringWithFormat:@"s%@.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:@"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
        [thisView.view setFrame:frame];
        [imageArray2 addObject:thisView];
    }

    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
    for(WeatherViewController *iv in imageArray2)
    {
        [self.scrollView addSubview:iv.view];
    }

希望这会帮助你。

于 2013-10-18T08:45:27.060 回答