0

我已将 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

4 回答 4

1

您已将所有视图添加到滚动中,因此,从滚动视图中枚举 UIView 并将您的 anotherSubview 添加到您的 UIView

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

   [myView addSubview:anotherSubview];
  }
于 2013-08-21T10:04:19.993 回答
0

NSlog(@"子视图:%@",self.scrollView.subviews);

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

   // NSLog(@"View : %@",view);
    // you can add ur subview in this view
}
于 2013-08-21T10:06:24.473 回答
0

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

注意:当且仅当该超级视图仅包含添加的视图作为子视图时,所有其他答案才可能起作用。如果存在任何其他视图,它也会在其中添加一个视图

于 2013-08-21T10:04:47.327 回答
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];

}
于 2013-08-21T09:57:16.353 回答