azamsharp
azamsharp

Reputation: 20068

Adding Items to UIScrollView Dynamically

I am getting crazy on this! I have the following code and it does not add anything to the UIScrollView. The view when rendered in emulator appears complete white.

- (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];


}

UPDATE 1:

For some reason even adding a view with background color does not seems to appear. I just see a white screen.

@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];


}

Upvotes: 1

Views: 5075

Answers (2)

Tanuj Jagoori
Tanuj Jagoori

Reputation: 309

- (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);


}

Upvotes: 0

Ben Scheirman
Ben Scheirman

Reputation: 40951

Nothing's jumping out at me.

Here are a few things to check:

  • What is the final frame of the scrollview if you log it out?
  • You are adding each imageView to the same exact spot, not offsetting by any amount.
  • The images you are creating might not exist. Perhaps try with simple views with different background colors?

Upvotes: 1

Related Questions