我需要以编程方式创建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];
答案 0 :(得分:1)
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame];
到
viewWithPictures=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.bottomView.frame.size.width,self.bottomView.frame.size.height)];
和这个
btn.frame=CGRectMake(self.bottomView.frame.origin.x+i*160, self.bottomView.frame.origin.y, 150, 100);
到
btn.frame=CGRectMake(i*160, self.bottomView.frame.origin.y, 150, 100);
这只是一个建议。
答案 1 :(得分:1)
当您设置将成为子视图的视图的帧时,您需要引用它将添加到的视图的边界。所以我认为你需要改变几行:
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);
答案 2 :(得分:0)
如果没有剩下的代码,很难说它只是因为你没有将容器视图(bottomView)添加到视图中?您可以在最后添加此内容:[self.view addSubview: bottomView]