0

I'm creating a UIView that allows the user to vote using a classical 5 stars rate system. My code works and everything is fine but I would like to make a preview on the storyboard using IBInspectable variables.

I really do not know how to achieve that result: I have two IBInspectable properties, an UIImage and an int. The int tells me how many "stars" I would like to show in the storyboard and the UIImage would choose the image used in the app.

@property (strong, nonatomic) IBInspectable UIImage *emptyStarImage;

@property (nonatomic) IBInspectable int numberOfStars;

This is the the thing I'm trying to achieve:

float usableWidth = self.frame.size.width - kHorizontalPadding * (self.numberOfStars - 1);
float maxStarSize = MIN(self.frame.size.height - 2 * kVerticalPadding, usableWidth / self.numberOfStars);

for (int i = 0; i < self.numberOfStars; i++) {
    
    float xStarPos = kHorizontalPadding * i + (i*maxStarSize);
   
    UIImageView *starView = [[UIImageView alloc] initWithFrame:CGRectMake(xStarPos, self.frame.size.height/2, maxStarSize, maxStarSize)];
    starView.image = self.emptyStarImage;
    [self addSubview:starView];
}

Thank you!

Community
  • 1
  • 1
DungeonDev
  • 1,176
  • 1
  • 12
  • 16

0 Answers0