我需要以编程方式创建一个 UIButton
,然后将其放在创建的 UIScrollView
上,然后将 UIScrollView
放在 UIView 上
。如果将这些元素添加到 self.view
它们会显示,但是当我想嵌套时它们不会显示。
这是我目前所拥有的:
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];
最佳答案
当您设置将成为 subview 的 View 的框架时,您需要引用它将添加到的 View 的边界。所以我认为您需要更改几行:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
应该是:
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.bounds];
和
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
应该是:
btn.frame=CGRectMake(i*160, 0, 150, 100);
关于iphone - 将 UIButton 添加到 UIScrollView,然后将 UIScrollView 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928395/