我到处寻找。试图增加这条线的厚度。无论如何以编程方式执行此操作?感谢
答案 0 :(得分:12)
唯一的方法是将separtorStype设置为UITableViewCellSeparatorStyleNone,然后您有两种选择:
答案 1 :(得分:6)
private let kSeparatorId = 123
private let kSeparatorHeight: CGFloat = 1.5
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
if cell.viewWithTag(kSeparatorId) == nil //add separator only once
{
let separatorView = UIView(frame: CGRectMake(0, cell.frame.height - kSeparatorHeight, cell.frame.width, kSeparatorHeight))
separatorView.tag = kSeparatorId
separatorView.backgroundColor = UIColor.redColor()
separatorView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
cell.addSubview(separatorView)
}
}
答案 2 :(得分:1)
我的解决方案是为UIView或UITableViewCell添加扩展名。
extension UIView {
func addSeparator(ofHeight height : CGFloat) {
let lineView = UIView()
lineView.backgroundColor = .red
self.addSubview(lineView)
let constraintString = "V:|-\(self.frame.size.height - height)-[v0(\(height))]|"
self.addConstraintsWithFormat("H:|[v0]|", views: lineView)
self.addConstraintsWithFormat(constraintString, views: lineView)
}
//MARK: - Constraints Extension
func addConstraintsWithFormat(_ format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
} }
然后在您的自定义TableViewCell或任何想要添加底线的视图中使用它。
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.addSeparator(ofHeight: 1)
}
答案 3 :(得分:0)
将table separatorsType设置为UITableViewCellSeparatorStyleNone,并根据需要为分隔符配置bg颜色的单元backroundView,并根据需要使用子视图对其进行掩码。 像这样:
UIView * bg = [[UIView alloc] initWithFrame:cell.bounds];
bg.backgroundColor = [UIColor darkGrayColor];
bg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIView * overBg = [[UIView alloc] initWithFrame:CGRectInset(cell.bounds, 0, 4.)];
overBg.backgroundColor = [UIColor whiteColor];
overBg.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[bg addSubview:overBg];
cell.backgroundView = bg;