0

enter image description here

I am trying to fill cells in a calendar with green boxes, all with different heights. I create an UIImage object by doing

- (UIImage *)imageWithColor:(UIColor *)color width:(float)w height:(float)h {
    CGRect rect = CGRectMake(0, 0, w, h);
    // Create a w by H pixel context
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);   // Fill it with your color
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

and set cells' backgrounds with

for (int i = 0; i < numDatesSelected; i++) {
    UIButton *myButton = (UIButton *)[self.smallCalendarView viewWithTag:[selectedDates[i] timeIntervalSince1970]];
    float btnWidth = myButton.frame.size.width;
    float btnHeight = (myButton.frame.size.height * 0.1 * i);
    UIImage *img = [self imageWithColor:[UIColor greenColor] width:btnWidth height:btnHeight];
    [myButton setBackgroundImage:img forState:(UIControlStateNormal)];
}

However, like you see in the screenshot, the heights are not very different from one another and I don't understand the reason. If I set width and height as 1 and 1 the rectangles still fill the entire cell whereas I think it should only fill a tiny portion (1x1) of the table cell because it has 53x40 size. Does anyone have an idea?

Maximus S
  • 10,759
  • 19
  • 75
  • 154

1 Answers1

1

Make sure the contentVerticalAlignment and contentHorizontalAlignment properties of your UIButton are NOT set to UIControlContentHorizontalAlignmentFill and UIControlContentVerticalAlignmentFill

Also, check this answer out https://stackoverflow.com/a/13093769/4171538. It might explain your problem

Community
  • 1
  • 1
anthonyliao
  • 1,314
  • 1
  • 8
  • 8