xcode 是否可以增加 UITableView 分隔线的粗细?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28646668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to increase the UITableView separator line thickness?
提问by Kex
I have looked everywhere. Trying to increase the thickness of this line. Is there anyway to do this programmatically? Thanks
我到处找。试图增加这条线的粗细。有没有办法以编程方式做到这一点?谢谢
回答by jherran
The only way of doing it, is setting the separtorStypeto UITableViewCellSeparatorStyleNoneand then you have two options:
唯一的方法是将separtorStype设置为UITableViewCellSeparatorStyleNone然后你有两个选择:
- Create a custom UITableViewCellwith the separator inside it or
- Create an alternate UITableViewCellwith the separator you want and place inside every other cells. If you want to display 3 rows on your table, you should display 5 instead with the alternate cell in rows 2 and 4.
- 创建一个带有分隔符的自定义UITableViewCell或
- 使用您想要的分隔符创建一个备用UITableViewCell并将其放置在每个其他单元格内。如果要在表格中显示 3 行,则应显示 5 行,并在第 2 行和第 4 行中显示备用单元格。
回答by ChikabuZ
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)
}
}
回答by Russell Warwick
My solution to this was to add an extension to either UIView or a UITableViewCell.
我对此的解决方案是向 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))
} }
Then use it within your custom TableViewCell or any View you would like to add a bottom line to.
然后在您的自定义 TableViewCell 或您想要添加底线的任何视图中使用它。
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.addSeparator(ofHeight: 1)
}
回答by Jim75
Set the table separatorsType to UITableViewCellSeparatorStyleNone and configure a cell backroundView with bg color as you like it for the separators and a subview with which to mask it as much as you want. Something like this:
将 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;