Question

I have added 20 subviews to scrollview line by line as rows.

Code Snippet

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

Now how do I add another subview on these 20 subviews?

Was it helpful?

Solution

you have added all the view to your scroll so,enumerate UIView from scrollview and add your anotherSubview to your UIView

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

   [myView addSubview:anotherSubview];
  }

OTHER TIPS

Try to:

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

}

Keep an NSMutableArray and add all the reference in the first loop.Loop the array count and add the second view to the pointer references in array

Note : all the other answer might work if and only if that superview contains only that added views as subviews alone.If anyother view exist it will add a view in there also

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

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

   // NSLog(@"View : %@",view);
    // you can add ur subview in this view
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top