2017-04-20 130 views
0

我想表这样的形象,显示顶部和底部边框颜色只有 enter image description hereios:如何在tableview中添加顶部和底部边框?

我有适用于viewDidLoad中()这个代码

tblFilter.layer.borderWidth = 0.5 
tblFilter.layer.borderColor = UIColor.white.cgColor 

上面的代码在加边境所有我不想要的一面。 我只想在顶部或底部的表格边框。 我想要像给定的图像的tableview边框。看到给定的图像。 请给我建议的解决方案 感谢

+0

您可以使用与1px高度和宽度相同的uiview。并设置uiview的背景颜色 – KAR

+0

我是新的ios所以,如何在顶部和底部添加uiview? – hardik

+0

试试这个:http://stackoverflow.com/a/23157272/3901620 – KKRocks

回答

0

试试这个

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) 
+1

谢谢,但当我滚动我的tableview它的滚动。我想修复顶部和底部。我做的事 ? – hardik

+0

@hardik检查更新的ans。最后一行是 –

+0

“btn”是什么意思? – hardik

0

如果不想使用表页脚和页眉,您可以使用表头视图和页脚视图,您可以与返回查看白色和高度将是1和宽度相同表格的宽度

+0

我这样做了,但它滚动表的内容。我想修复顶部和底部。 – hardik

0

使用的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的背景和设置颜色。这也将有所帮助。

+0

但如何停止滚动? – hardik

+0

您要求的内部单元格粘贴所有4个约束顶部,底部,顶部,尾部。 – Jitendra

+0

不,我想要表格视图的顶部和底部边框,我做了它,但我滚动表格视图时,我的表格边框滚动,所以,如何停止边框滚动 – hardik

0

取两个UIView。给它白色的颜色。 顶级用户的坐标是可用视图的最小值,底部用户的坐标是可用视图的最大值。

您可以使用GetMaxYGetMinY得到最小y和最大值为y。

宽度两者的UIView是相同的UITableView。

高度两者的UIView是1px的。

希望这会帮助你。在高度值1单元格

0

取标签,并给边境它。以编程方式隐藏/显示您的需求索引。

1

我的问题解决了。 我添加了一个自定义视图。在该视图中,我添加了两个视图,顶部和底部的视图为1px高度,我在该自定义视图中添加了table-view。

相关问题