- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, rect);
if ( self.tag == 0) {
//上分割线,
CGContextSetStrokeColorWithColor(context, [ UIColor colorWithRed:196.0f/255.0f green:196.0f/255.0f blue:196.0f/255.0f alpha:1.0f ].CGColor);
CGContextStrokeRect(context, CGRectMake(0, -0.5f, rect.size.width, 0.5f));
}
//下分割线
CGContextSetStrokeColorWithColor(context, [ UIColor colorWithRed:196.0f/255.0f green:196.0f/255.0f blue:196.0f/255.0f alpha:1.0f ].CGColor);
CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width , 0.5f));
}
或
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, rect);
CGContextSetStrokeColorWithColor(context, ColorSystemDefultDividerLine.CGColor);
CGContextStrokeRect(context, CGRectMake(Spacing, rect.size.height - 0.25, rect.size.width, 0.25));
}
本文介绍如何在iOS开发中使用UIView自定义上下分割线的绘制方法。通过CGContext进行精确控制,实现不同颜色及位置的线条效果,适用于各种UI界面需求。

被折叠的 条评论
为什么被折叠?



