2012-01-19 44 views
1

をプログラムで作成し、それをUIScrollViewに配置してUIViewに配置する必要があります。これらの要素をself.viewに追加すると表示されますが、ネストするときは表示されません。ハードUIScrollViewにUIButtonを追加し、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)を追加されていないことが考えられます。ここでは は、私がこれまで持っているものでしょうか?

viewWithPictures=[[UIScrollView alloc] initWithFrame:CGRectMake(0,0,self.bottomView.frame.size.width,self.bottomView.frame.size.height)]; 

から[self.view addSubview: bottomView]

+0

おかげで、 =) –

1
viewWithPictures=[[UIScrollView alloc] initWithFrame:self.bottomView.frame]; 

この

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

にこれは単なる提案です:あなたはどちら最後にこれを追加することによって行うことができます。

関連する問題