2012-01-19 144 views
1

我需要以编程方式创建UIButton,然后将它放在创建的UIScrollView上,然后将UIScrollView放在UIView上。如果将这些元素添加到self.view中,则会显示它们,但是当我想嵌套时,则不会显示它们。 这是我到目前为止有:将UIButton添加到UIScrollView,然后将UIScrollView添加到UIView

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]; 

回答

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); 
0

很难说没有代码的其余部分,但它可能仅仅是因为你没有加入你的容器视图(bottomView)你的看法?您可以通过在年底加入该做的事:[self.view addSubview: bottomView]

+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); 

这仅仅是一个建议。