【发布时间】:2015-04-23 04:13:35
【问题描述】:
我到处找。试图增加这条线的粗细。反正有没有以编程方式做到这一点?谢谢
【问题讨论】:
标签: ios xcode uitableview
我到处找。试图增加这条线的粗细。反正有没有以编程方式做到这一点?谢谢
【问题讨论】:
标签: ios xcode uitableview
我对此的解决方案是向 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)
}
【讨论】:
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)
}
}
【讨论】:
将表格 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;
【讨论】:
这样做的唯一方法是将separtorStype 设置为UITableViewCellSeparatorStyleNone,然后您有两个选择:
【讨论】: