Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically adding UIButtons and UIImages to a UIScrollview

I've modified Apple's Scrolling example to grab a series of images created from files referenced in CoreData objects. I can add the images fine, but I also want to programmatically add a UIButton to each image. The number of images depends on the number of CoreData objects, so I can't use IB.

Can anyone give me some help on how I would best implement this?

Here's the code that grabs the images from the CoreData store:

NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
    Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i];

    NSArray *paths       = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]];
    NSLog(@"imageName: %@", imageName);

    UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName];
    NSLog(@"image: %@", image);

    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    NSLog(@"imageView: %@", imageView);

    [imageView setContentMode:UIViewContentModeScaleAspectFit];
    [imageView setBackgroundColor:[UIColor blackColor]];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i;  // tag our images for later use when we place them in serial fashion
    [scrollView1 addSubview:imageView];

    [image release];
    [imageView release];
}

[self layoutScrollImages];  // now place the photos in serial layout within the scrollview

And here's the code that lays the images out in the scrollview:

- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];

// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    {
        CGRect frame = view.frame;
        frame.origin = CGPointMake(curXLoc, 0);
        view.frame = frame;

        curXLoc += (kScrollObjWidth);
    }
}

// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
like image 714
glenstorey Avatar asked Feb 24 '26 00:02

glenstorey


1 Answers

Not sure I understand your problem. Couldn't you just do this?

...
 imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = imageView.frame;
[aButton addTarget:self action:@selector(whatever:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];
like image 59
Joseph Tura Avatar answered Feb 26 '26 18:02

Joseph Tura



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!