在滚动视图中添加标签
Adding label in scrollView
我的问题:我有一个白色 window 但我不明白为什么。
我尝试了一些解决方案,但没有任何效果。
感谢您的帮助。
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];
}
您的 UIlabel 和 UIImageView 框架超出可见范围。
例如,如果您设置
CGRect rectImage = CGRectMake(0,i*30,320,30);
CGRect rectLabel = CGRectMake(10,(i*30)+40,320,30);
您将能够在滚动视图中看到它们。请正确设置框架以查看 UI 组件,并设置滚动视图的内容大小以使其滚动。如果你的内容尺寸比滚动视图框架大,滚动视图将能够滚动,
感谢您的回答。
现在可以了。如果您对我有什么建议,我会很高兴。
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];
}
我的问题:我有一个白色 window 但我不明白为什么。 我尝试了一些解决方案,但没有任何效果。 感谢您的帮助。
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];
}
您的 UIlabel 和 UIImageView 框架超出可见范围。
例如,如果您设置
CGRect rectImage = CGRectMake(0,i*30,320,30);
CGRect rectLabel = CGRectMake(10,(i*30)+40,320,30);
您将能够在滚动视图中看到它们。请正确设置框架以查看 UI 组件,并设置滚动视图的内容大小以使其滚动。如果你的内容尺寸比滚动视图框架大,滚动视图将能够滚动,
感谢您的回答。 现在可以了。如果您对我有什么建议,我会很高兴。
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];
}