ios 改变cell分割线高度,在iOS 7的UITableViewCell之间绘制自定义分隔线

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值