ios:如何在tableview中添加顶部和底部边框?
问题描述:
我想表这样的形象,显示顶部和底部边框颜色只有 ios:如何在tableview中添加顶部和底部边框?
我有适用于viewDidLoad中()这个代码
tblFilter.layer.borderWidth = 0.5
tblFilter.layer.borderColor = UIColor.white.cgColor
上面的代码在加边境所有我不想要的一面。 我只想在顶部或底部的表格边框。 我想要像给定的图像的tableview边框。看到给定的图像。 请给我建议的解决方案 感谢
答
试试这个
let topBorder = CAShapeLayer()
let topPath = UIBezierPath()
topPath.move(to: CGPoint(x: 0, y: 0))
topPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: 0))
topBorder.path = topPath.cgPath
topBorder.strokeColor = UIColor.red.cgColor
topBorder.lineWidth = 1.0
topBorder.fillColor = UIColor.red.cgColor
tblFilter.layer.addSublayer(topBorder)
let bottomBorder = CAShapeLayer()
let bottomPath = UIBezierPath()
bottomPath.move(to: CGPoint(x: 0, y: tblFilter.frame.height))
bottomPath.addLine(to: CGPoint(x: tblFilter.frame.width, y: tblFilter.frame.height))
bottomBorder.path = bottomPath.cgPath
bottomBorder.strokeColor = UIColor.red.cgColor
bottomBorder.lineWidth = 1.0
bottomBorder.fillColor = UIColor.red.cgColor
tblFilter.layer.addSublayer(bottomBorder)
答
使用的tableview页眉和页脚。
// in -viewDidLoad
self.tableView.tableHeaderView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1/UIScreen.mainScreen.scale)];
line.backgroundColor = self.tableView.separatorColor;
line;
});
同样做脚注也。
self.tableView.tableFooterView = ({UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 1/UIScreen.mainScreen.scale)];
line.backgroundColor = self.tableView.separatorColor;
line;
});
否则只需添加UIView的背景和设置颜色。这也将有所帮助。
答
取两个UIView。给它白色的颜色。 *用户的坐标是可用视图的最小值,底部用户的坐标是可用视图的最大值。
您可以使用GetMaxY
和GetMinY
得到最小y和最大值为y。
宽度两者的UIView是相同的UITableView。
高度两者的UIView是1px的。
希望这会帮助你。在高度值1单元格
答
取标签,并给边境它。以编程方式隐藏/显示您的需求索引。
答
我的问题解决了。 我添加了一个自定义视图。在该视图中,我添加了两个视图,顶部和底部的视图为1px高度,我在该自定义视图中添加了table-view。
您可以使用与1px高度和宽度相同的uiview。并设置uiview的背景颜色 – KAR
我是新的ios所以,如何在顶部和底部添加uiview? – hardik
试试这个:http://*.com/a/23157272/3901620 – KKRocks