动态地向UIScrollView添加项目

时间:2012-05-01 19:35:34

标签: ios

我对此感到疯狂!我有以下代码,它不会向UIScrollView添加任何内容。在模拟器中呈现时的视图显示为完全白色。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add scroll view to the view 

      self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 200,self.view.frame.size.width,110)];

    self.scrollView.contentSize = CGSizeMake(400, 80);
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setPagingEnabled:YES];



    NSArray *imageNames = [NSArray arrayWithObjects:@"kidontrike_4.png",@"mockup_car2.png",@"mockup_car1.png", nil];

    for(NSString *imageName in imageNames) 
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

        imageView.frame = CGRectMake(0, 0, 100, 100);

        [self.scrollView addSubview:imageView];
    }


    [self.view addSubview:self.scrollView];


}

更新1:

出于某种原因,甚至没有添加带有背景颜色的视图。我只看到一个白色的屏幕。

@property (nonatomic,weak)  UIScrollView *scrollView; 

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add scroll view to the view 

      self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];

    self.scrollView.contentSize = CGSizeMake(80, 80);
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setPagingEnabled:YES];


    UIView *someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    [someView setBackgroundColor:[UIColor greenColor]];

    [self.scrollView addSubview:someView];
    [self.view addSubview:self.scrollView];


}

2 个答案:

答案 0 :(得分:1)

没有什么东西在跳我。

以下是一些要检查的事项:

  • 如果您将其注销,滚动视图的最终帧是什么?
  • 您正在将每个imageView添加到同一个精确位置,而不是按任何数量进行抵消。
  • 您正在创建的图像可能不存在。也许尝试使用不同背景颜色的简单视图?

答案 1 :(得分:0)

- (void)viewDidLoad
{
    [super viewDidLoad];

    // add scroll view to the view 

      self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 200,self.view.frame.size.width,110)];

    [self.view addSubview:self.scrollView];
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setPagingEnabled:YES];

    NSArray *imageNames = [NSArray arrayWithObjects:@"kidontrike_4.png",@"mockup_car2.png",@"mockup_car1.png", nil];
 UIImageView *imageView;
    for(NSString *imageName in imageNames) 
    {
       imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];

        imageView.frame = CGRectMake(0, 0, 100, 100);

        [self.scrollView addSubview:imageView];
    }

   self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, imageView.frame.size.height+imageView.frame.origin.y+100);


}