我正在开发一个在 Imageview 上显示多个图像的 View ,并且图像的数量是动态的。 我通过 URL 获取这些图像,并且 URL 的字符串存储在数组中。
我采用了 for 循环来为要在其上显示的每个图像创建一个新的 ImageView 。 ImageView 的框架是根据图像的大小设置的。但问题是,当图像数量超过 5 时,第六张图像就会从屏幕中消失。但如果数组中的图像超过 5 个,我希望它向下移动。
下面是我使用 for 循环在 imageview 上显示图像的代码
int x=0;
for (int i=0;i<[items count]; i++)
{
NSLog(@"image name %@",[items objectAtIndex:i]);
NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);
NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];
UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
imgV.frame=CGRectMake(x, 0, 50, 50);
[cell.contentView addSubview:imgV];
x=x+imgV.frame.size.width+10;
}
最佳答案
按此图像移动
int x=0;
int y =0;
for (int i=0;i<[items count]; i++)
{
NSLog(@"image name %@",[items objectAtIndex:i]);
NSLog(@"image url %@",[NSURL URLWithString:[NSString stringWithFormat:@"%@",[items objectAtIndex:i]]]);
NSString* urlTextEscaped = [[NSString stringWithFormat:@"%@",[items objectAtIndex:i]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:urlTextEscaped]];
UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageWithData:imgData]];
imgV.frame=CGRectMake(x, y, 50, 50);
[cell.contentView addSubview:imgV];
if(x>cell.frame.size.width)
{
x =0;
y= y+imgV.frame.size.height+10;
}
else
{
x=x+imgV.frame.size.width+10;
}
}
关于ios - 如果 subview 超出 ios 中的屏幕边界,如何向下移动 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44674474/