Uiscrollview无法添加uilabels

时间:2013-12-18 11:19:05

标签: ios objective-c uiscrollview

我正在尝试将uilabels添加到uiscrollview中。我使用for循环创建20个标签,然后在滚动视图中逐个添加它们。没有用。这是我的代码

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

我正在使用故事板。我也试过alloc init但它仍然失败了。我确实在viewdidload中设置了委托。我更改了scrollview的背景图像,以检查它是否在屏幕上,它是。

编辑:现在可以使用了,我所做的只是重启Xcode

2 个答案:

答案 0 :(得分:0)

试试这个。 。 。

您正在将循环中的滚动视图添加20次到uiview。这也是你代码的错误部分

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

   }
}

答案 1 :(得分:0)

试试这个:

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

    }