【发布时间】:2014-11-11 06:42:32
【问题描述】:
我正在尝试用绿色框填充日历中的单元格,所有单元格都具有不同的高度。我通过做来创建一个 UIImage 对象
- (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;
}
并设置单元格的背景
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)];
}
但是,就像您在屏幕截图中看到的那样,高度彼此之间并没有太大差异,我不明白原因。如果我将宽度和高度设置为 1 和 1,矩形仍然会填充整个单元格,而我认为它应该只填充表格单元格的一小部分 (1x1),因为它的大小为 53x40。有人有想法吗?
【问题讨论】:
标签: ios objective-c