Rob Bonner
Rob Bonner

Reputation: 9426

UIView subview placement

I have a UIView that I am placing UIImageView's into, with each UIImageView being 16 pixels wide.

The query is if I add say 5 of the UIImageViews using addSubView, and the UIView is spec'd at 16 tall by 300 wide, is there any way to have the images in the view 'stack up' so to speak, so that the images are not on top of each other? Like adding image tags to a web page.

Upvotes: 0

Views: 1629

Answers (2)

Michael
Michael

Reputation: 456

I think I understand your question correctly. You want all the images to line up in a row correct? You would need to set the frame of the UIImageView's view. The orgin will be where the top left of your image is(x,y coordinates inside the UIView that contains it) - so you would move that over 16 each time you add another image.

Upvotes: 1

Dan F
Dan F

Reputation: 17732

The easiest way I see of doing this is to keep a running tally of where you last placed an image, then increment by width, something like this:

-(void) addImage:(UIImage*)img toView:(UIView*)view
{
    static CGRect curFrame = CGRectMake (0,0,16,16);
    UIImageView* imgView = [[UIImageView alloc] initWithFrame:curFrame];
    imgView.image = img;
    [view addSubview:imgView];

    curFrame.origin.x += 16;
}

This will have the images appear within your view from left to right

Upvotes: 2

Related Questions