I am trying to add uilabels to a uiscrollview. I use a for loop to create 20 labels and then add them one by one in the scroll view. Does no work. Here is my code
-(void)setScrollViews{
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:@"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
[self.activitiesScrollView addSubview:label];
[self.view addSubview:self.activitiesScrollView];
}
}
I am using storyboard. I also tried alloc init
but it still failed. I did set the delegate in the viewdidload
. I changed the background image for the scrollview to check if it is there on the screen and it is.
EDIT:This works now, All I did was restart Xcode
try this . . .
you are adding scrollview 20 times to the uiview in the loop . that is also wrong part of your code
-(void)setScrollViews{
[self.view addSubview:self.activitiesScrollView];
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:@"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView addSubview:label];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
}
}
try this:
-(void)setScrollViews{
for (int i = 0; i < 80; i++)
{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 20 * i,160 , 30)];
label.font = [UIFont systemFontOfSize:12.0];
label.textColor = [UIColor blueColor];
label.text = [NSString stringWithFormat:@"Label number %d", i ];
label.numberOfLines = 0;
[label sizeToFit];
[self.activitiesScrollView setContentSize:CGSizeMake(320, self.activitiesScrollView.frame.size.height + 20)];
[self.activitiesScrollView addSubview:label];
}
[self.view addSubview:self.activitiesScrollView];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.