max_
max_

Reputation: 24481

Add padding to elements in a UIScrollView

I am trying to create a grid UIScrollView, which so far is working great, I am just having a little trouble with adding 20px padding to both the width and height of the UIImageViews being displayed in the `UIScrollView``.

At the moment, I am adding 20px (px & py) to both the x and y values of the UIImageView's frame. However, this just moves the rows along 20px, and the columns down 20px. Please can you tell me where I am going wrong?

int x = 0;
int y = 0;
int w = 65;
int h = 65;
int c = 0;
int r = 0;
int px = 20;
int py = 20;

for (int i = 0; i < [self.content count]; i++) {

    if (c < 4)
        c++;
    else if (c == 4) {
        c = 0;
        r++;
    }

    x = c * w;

    y = r * h;

    x += px;

    y += py;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)];

    [imageView setImage:[self.content objectAtIndex:i]];

    [imageView setContentMode:UIViewContentModeScaleToFill];

    [self.scrollView addSubview:imageView];

    [imageView release];

}

Upvotes: 1

Views: 943

Answers (1)

Husker Jeff
Husker Jeff

Reputation: 857

You want the padding between each image, right? As it stands, you're basically only adding it in once, at the very top. Instead, add the padding, then multiply by the row and column. Each image should be spaced out then.

EDIT to correct a silly mistake on my part.

x = c * (w + px);
y = r * (h + py);

Upvotes: 1

Related Questions