添加到UIScrollView时,UIViews为空

时间:2013-10-18 08:30:06

标签: ios uiview uiscrollview

我正在尝试将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];
}

有什么建议吗?

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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];
    }

希望这会对您有所帮助。