在scrollview的子视图上添加子视图

时间:2013-08-21 09:44:51

标签: ios subview

我已将滚动视图的20个子视图逐行添加为行。

代码段

yPos=0;
for (int i=0; i<20; i++)
{
    UIView *timeView=[[UIView alloc]initWithFrame:CGRectMake(71, yPos, 909, 60)];

    timeView.userInteractionEnabled=TRUE;
    timeView.exclusiveTouch=YES;
    timeView.tag=i;

    NSLog(@"sub vieww tag=:%d",timeView.tag);

    timeView.backgroundColor=[UIColor whiteColor];

    UILabel *lbltime=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 60)];
    lbltime.text=@"VIEW HERE";
    lbltime.textColor=[UIColor grayColor];

    [timeView addSubview:lbltime];

    [scrlView addSubview:timeView];

    yPos=yPos+61;
}

现在如何在这20个子视图中添加另一个子视图?

4 个答案:

答案 0 :(得分:1)

您已将所有视图添加到滚动条中 所以,从scrollview枚举UIView并将你的anotherSubview添加到你的UIView

for(UIView *myView in scrollview.subviews)
  {
   //Create UIView
   UIView *anotherSubView=[UIView alloc]initWithFrame:CGRectMake(0,0,20,20)];

   [myView addSubview:anotherSubview];
  }

答案 1 :(得分:0)

尝试:

for (int i=0; i<20; i++)
{
UIView *timeView=[self subViews] objectAtIndex:i];  // or super, I didn't underdtand question exactly

CGRect yourFrameOfSub=CGRectMake(0,0,10,10); //set your values
UIView *anotherSubview=[UIView alloc]initWithFrame:yourFrameOfSub];

[timeView addSubview:anotherSubview];

}

答案 2 :(得分:0)

保留NSMutableArray并在第一个循环中添加所有引用。接收数组计数并将第二个视图添加到数组中的指针引用

注意:当且仅当该超级视图仅包含作为子视图添加的视图时,所有其他答案都可能有效。如果存在任何其他视图,则会在其中添加视图

答案 3 :(得分:0)

NSlog(@“Subviews:%@”,self.scrollView.subviews);

for(UIView *view in self.scrollView.subviews){

   // NSLog(@"View : %@",view);
    // you can add ur subview in this view
}