在scrollView中添加标签

时间:2015-01-15 18:17:00

标签: ios uiscrollview

我的问题:我有一个白色的窗户,我不明白为什么。 我尝试了一些解决方案,但没有任 谢谢你的帮助。

CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
[self.view addSubview: monScrollView];

for (int i = 0; i < 10; i++)
{

    CGRect rectImage = CGRectMake(0,longueurDevice/2+i*30,320,30); // Définition d'un rectangle
    CGRect rectLabel = CGRectMake(10,longueurDevice/2+i*30,320,30);

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
    imageView.image = [UIImage imageNamed:@"barreTest.png"];
    [monScrollView addSubview: imageView];

    UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
    monLabel.text = @"Ceci est un label";
    [monScrollView addSubview: monLabel];
}

2 个答案:

答案 0 :(得分:0)

您的UIlabel和UIImageView框架位于可见边界之外。

例如,如果你设置

CGRect rectImage = CGRectMake(0,i*30,320,30);
CGRect rectLabel = CGRectMake(10,(i*30)+40,320,30);

您将能够在滚动视图中看到它们。请正确设置框架以查看UI组件,并设置滚动视图的内容大小以使其滚动。如果滚动视图框的内容大小较大,滚动视图将能够滚动

答案 1 :(得分:0)

感谢您的回答。 现在它有效。如果您对我有任何建议,我会很高兴。

int max = 10;
CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
monScrollView.contentSize=CGSizeMake(largeurDevice,30*max);
[self.view addSubview: monScrollView];

for (int i = 0; i < max; i++)
{
    CGRect rectImage = CGRectMake(0,i*30,largeurDevice,30); // Définition d'un rectangle
    CGRect rectLabel = CGRectMake(10,i*30,largeurDevice,30);
    CGRect rectBututton = CGRectMake(largeurDevice-20,longueurDevice/2+i*30,10,10);

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
     imageView.image = [UIImage imageNamed:@"barreTest.png"];
    [monScrollView addSubview: imageView];

    UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
    monLabel.text = @"Ceci est un label";
    [monScrollView addSubview: monLabel];
}