2017-04-20 168 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。

相關問題