gpt4 book ai didi

iOS drawRect 没有正确读取颜色属性

转载 作者:行者123 更新时间:2023-11-29 01:55:48 26 4
gpt4 key购买 nike

我有一个 UITableViewCell 的子类,我正在尝试在 drawRect 中画一条线。

有一个属性separatorColor在子类的标题中:

@interface DXDecisionsTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *titleLabel;
@property (nonatomic, strong) UIColor *separatorColor;
@end

drawRect 看起来像这样:

- (void) drawRect:(CGRect)rect
{
[super drawRect:rect];

CGContextRef context = UIGraphicsGetCurrentContext();

CGRect cellBounds = self.bounds;
CGRect separatorRect = CGRectMake(0, cellBounds.size.height - kDXDecisionsTableViewCellSeparatorHeight, cellBounds.size.width, kDXDecisionsTableViewCellSeparatorHeight);

NSAssert(self.separatorColor, @"separator color not set");
NSLog(@"%@", [self.separatorColor stringValue]);
CGContextSetFillColorWithColor(context, self.separatorColor.CGColor);
//[self.separatorColor setFill];
//[[UIColor blueColor] setFill];
//CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
//self.separatorColor = [UIColor redColor];
//CGContextSetFillColorWithColor(context, self.separatorColor.CGColor);
CGContextFillRect(context, separatorRect);

//NSLog(@"self is %p", self);
}

如果我在 drawRect 中设置颜色然后它起作用了,我不知道为什么。

如果我打印出 separatorColor 的值我得到#c7c7cc ,即使在初始化程序中它是 #7f00ffb2这是正确的。

初始化器:

-(void)awakeFromNib {
// Initialization code

self.separatorColor = [UIColor colorWithRed:0.5f green:0.0f blue:1.0f alpha:0.7f];
NSLog(@"%@", [self.separatorColor stringValue]);
}

我正在模拟器上测试。

最佳答案

将您的属性名称更改为 separatorColor2 - 这应该可以防止 Apple 自己的代码设置您的 separatorColor

关于iOS drawRect 没有正确读取颜色属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30871290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com