Reputation: 1954
I need to create a UIButton
programatically and then put it on a created UIScrollView
and then put the UIScrollView
on a UIView
. If add these elements to self.view
they are displayed, but when I want to nest then they are not displayed.
Here is what I have so far:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
viewWithPictures.contentSize=CGSizeMake(160*[smallImagesFromGallery count], self.bottomView.frame.size.height);
viewWithPictures.backgroundColor=[UIColor greenColor];
NSLog(@"Number of small images: %i",[smallImagesFromGallery count]);
for(int i=0; i<[smallImagesFromGallery count]; i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
[btn setBackgroundImage:[smallImagesFromGallery objectAtIndex:i] forState:UIControlStateNormal];
if (btn==nil) {
NSLog(@"Button is nil");
}
btn.tag=i;
[btn addTarget:self action:@selector(viewLargeVersion:) forControlEvents:UIControlEventTouchUpInside];
[viewWithPictures addSubview:btn];
}
[bottomView addSubview:viewWithPictures];
Upvotes: 1
Views: 1044
Reputation: 878
When you're setting the frame of a view that will become a subview, you need to reference the bounds of the view that it will be added to. So I think you need to change a couple of lines:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
should be:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.bounds];
and
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
should be:
btn.frame=CGRectMake(i*160, 0, 150, 100);
Upvotes: 1
Reputation: 2256
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
to
viewWithPictures=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.bottomView.frame.size.width,self.bottomView.frame.size.height)];
and this
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
to
btn.frame=CGRectMake(i*160, self.bottomView.frame.origin.y, 150, 100);
This is just a suggestion.
Upvotes: 1
Reputation: 15400
Hard to tell without the rest of the code but could it just be that you aren't adding your container view (bottomView) to your view? Which you can do by adding this at the end: [self.view addSubview: bottomView]
Upvotes: 0