Customizable background view for UITableViewCell in grouped table view
// | |
// CellBackgroundView.h | |
// | |
// | |
#import <UIKit/UIKit.h> | |
typedef enum { | |
CellPositionTop, | |
CellPositionMiddle, | |
CellPositionBottom, | |
CellPositionSingle | |
} CellPosition; | |
@interface CellBackgroundView : UIView | |
@property(assign) CellPosition position; | |
@property(strong) UIColor *fillColor; | |
@property(strong) UIColor *borderColor; | |
@end |
// | |
// CellBackgroundView.m | |
// | |
// | |
#import "CellBackgroundView.h" | |
static const CGFloat kCornerRadius = 10; | |
@implementation CellBackgroundView | |
@synthesize position, fillColor, borderColor; | |
- (void)drawRect:(CGRect)rect | |
{ | |
CGRect bounds = CGRectInset(self.bounds, | |
0.5 / [UIScreen mainScreen].scale, | |
0.5 / [UIScreen mainScreen].scale); | |
UIBezierPath *path; | |
if (position == CellPositionSingle) { | |
path = [UIBezierPath bezierPathWithRoundedRect:bounds cornerRadius:kCornerRadius]; | |
} else if (position == CellPositionTop) { | |
bounds.size.height += 1; | |
path = [UIBezierPath bezierPathWithRoundedRect:bounds | |
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight | |
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)]; | |
} else if (position == CellPositionBottom) { | |
path = [UIBezierPath bezierPathWithRoundedRect:bounds | |
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight | |
cornerRadii:CGSizeMake(kCornerRadius, kCornerRadius)]; | |
} else { | |
bounds.size.height += 1; | |
path = [UIBezierPath bezierPathWithRect:bounds]; | |
} | |
[self.fillColor setFill]; | |
[self.borderColor setStroke]; | |
[path fill]; | |
[path stroke]; | |
} | |
@end |
This comment has been minimized.
This comment has been minimized.
Comment above has typo |
This comment has been minimized.
This comment has been minimized.
Nice! |
This comment has been minimized.
This comment has been minimized.
There is one thing missing though: |
This comment has been minimized.
This comment has been minimized.
Still very useful today. Thanks a lot! |
This comment has been minimized.
This comment has been minimized.
Thanks! This is very useful. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Sample usage: