KGViewSeparators is a small helper library that helps to quickly insert separators into a view. No subclassing required!
Ease of use and customization is not the only benefit. The library especially helps when it comes to cell reuse: it prevents multiple separators from being added to a single view, you can show/hide separators for any cell and more. All you have to worry about is specifying when you want to show/hide a separator and it will just work.
The library currently allows to modify the color, lineWidth and the insets of the separators.
Core Principles:
- Ease of use
- Customization
- Support for view reuse
To install using CocoaPods, add the following to your project Podfile:
pod 'KGViewSeparators'
Alternatively, drag and drop all of the files in the KGViewSeparators folder into your Xcode project, agreeing to copy files if needed.
#import <UIView+KGViewSeparators.h>
Enum:
KGViewSeparatorTop
KGViewSeparatorBottom
Objective-C:
[view kg_show:YES separator:KGViewSeparatorTop color:[UIColor blackColor] lineWidth:KGViewSeparatorLineWidth(1.0) insets:UIEdgeInsetsMake(0, 15.0, 0, 15.0)];
Swift:
view.kg_show(true, separator: .Bottom, color: UIColor.blackColor(), lineWidth: KGViewSeparatorLineWidth(1.0), insets: UIEdgeInsetsZero)
Helper function:
Line widths should be divided by the screen scale for proper pixel rendering.
CGFloat KGViewSeparatorLineWidth(CGFloat lineWidth);
Create your own category to simplify code.
@interface UITableViewCell (Separators)
- (void)showTopSeparator:(BOOL)show;
- (void)showBottomSeparator:(BOOL)show;
@end
#import <UIView+KGViewSeparators.h>
@implementation UITableViewCell (Separators)
- (void)showTopSeparator:(BOOL)show {
[self.contentView kg_show:show separator:KGViewSeparatorTop color:[UIColor blackColor] lineWidth:KGViewSeparatorLineWidth(1.0) insets:UIEdgeInsetsMake(0, 15.0, 0, 15.0)];
}
- (void)showBottomSeparator:(BOOL)show {
[self.contentView kg_show:show separator:KGViewSeparatorBottom color:[UIColor blackColor] lineWidth:KGViewSeparatorLineWidth(1.0) insets:UIEdgeInsetsMake(0, 15.0, 0, 15.0)];
}
@end
When adding separators to cells it's important to add separators to the contentView
.
KGViewSeparators works with Swift! Checkout the Swift example in the "Examples" folder.
KGViewSeparators uses associated objects to store the separator views and the layout is done via Auto Layout. Once stored, the views are simply shown or hidden. By storing the separator views, and the configurations that customize the separators, the library is able to reuse what was already created.
Created and maintained by Krisjanis Gaidis.
KGViewSeparators is released under the MIT license. See LICENSE for details.