Because I had some troubles using the default separator line between the UITableViewCell I want to use my own. Therefore I'm using auto layout. C# is the language I used. You can of course provide solutions in Objective-C.
In the constructor of my custom cell I add my view UIView:
separator = new DividerView ();
ContentView.Superview.AddSubview (separator);
One has to add it to the superview otherwise it doesn't cover the accessory area. In updateConstraints I set up my constraints:
separator.TranslatesAutoresizingMaskIntoConstraints = false;
this.ContentView.Superview.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|[separator]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.Superview.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|-(82@999)-[separator(1)]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
This for example, does work on iOS 8 but not on iOS 7. Also this constraint V:[separator(1)]| would work on iOS 8 but not on iOS 7.
解决方案
This code works for me (subclass of UITableViewCell):
- (void)awakeFromNib {
// Initialization code
[super awakeFromNib];
PCTableViewCellSeparatorView *sV = [[PCTableViewCellSeparatorView alloc] initWithFrame:CGRectMake(0, 0, 0, 1.0)]; // my custom subclass of UIView for drawing 1px line
sV.backgroundColor = [UIColor clearColor];
self.accessoryView.backgroundColor = [UIColor clearColor];
[sV setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.contentView addSubview:sV];
// horizontal
NSNumber *space = [NSNumber numberWithFloat:0.0f];
NSNumber *space2 = [NSNumber numberWithFloat:-64.0f]; // MAGIC HERE
NSDictionary *views = NSDictionaryOfVariableBindings(sV, self.contentView);
NSDictionary *metrics = NSDictionaryOfVariableBindings(space, space2);
NSString *horizontalFormat =@"|-space-[sV]-space2-|";
NSArray* horizontal = [NSLayoutConstraint constraintsWithVisualFormat:horizontalFormat options:NSLayoutFormatDirectionLeadingToTrailing
metrics:metrics
views:views];
// height
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:sV attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0f constant:1.0f];
// bottom
NSLayoutConstraint *vertical1 = [NSLayoutConstraint constraintWithItem: sV
attribute: NSLayoutAttributeBottom
relatedBy: NSLayoutRelationEqual
toItem: self.contentView
attribute: NSLayoutAttributeBottom
multiplier: 1.0f
constant: 0.0f
];
[self.contentView addConstraints:horizontal];
[self.contentView addConstraints:@[vertical1,height]];
self.separatorView = sV; // my own cell's property
}
Code of PCTableViewCellSeparatorView:
#import
@interface PCTableViewCellSeparatorView : UIView
@end
#import "PCTableViewCellSeparatorView.h"
#import "UIColor+Utilities.h"
@implementation PCTableViewCellSeparatorView
// -----------------------------------------------------------------------
#pragma mark - Drawing
// -----------------------------------------------------------------------
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGFloat inset;
switch ([UIScreen screenScale]) {
case ScreenScale2x:
inset = 0.5f/2.0f;
break;
case ScreenScale3x:
inset = 0.5f/3.0f;
break;
default:
inset = 0.5f;
break;
}
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
// draw
CGContextSetLineWidth(context, inset);
CGContextSetStrokeColorWithColor(context, [UIColor colorWithRGB:PCColorGrey].CGColor);
CGContextMoveToPoint(context, 0, CGRectGetHeight(rect)-inset);
CGContextAddLineToPoint(context, CGRectGetWidth(rect), CGRectGetHeight(rect)-inset);
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
@end