答案 0 :(得分:1)
您可以运行循环并将图像作为子视图添加到某个视图中。
-(UIView*)createRatingView :(NSInteger)stars {
UIView* rating = [[UIView alloc]initWithFrame:CGRectMake(0,0,120,20)]; //set proper frame.. In this example (20*5) + (5*4) where 20 is width of image and 5 is padding between stars
for (i=1;i<=5;i++) {
int xPos = (i-1)*(20+5); // assuming 20 is width of image and 5 is padding between 2 stars
UIImageView* img = [[UIImageView alloc]initWithFrame:CGRectMake(xPos,0,20,20)];
if (stars<= i) {
img.image = [UIImage named:@"goldStar"];
}
else {
img.image = [UIImage named:@"grayStar"];
}
[rating addSubview : img];
}
return rating;
}