简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM