hardik
hardik

Reputation: 199

ios:how to add top and bottom border in tableview?

I want table like this image that show top and bottom border with color only enter image description here

i have apply this code in viewDidLoad()

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

above code add border at all side that i dont want. i want table border in only top or bottom side. i want tableview border like given image. see given image. please suggest me solution Thanks

Upvotes: 0

Views: 5125

Answers (6)

hardik
hardik

Reputation: 199

my problem is solved. i added one custom view. In that view i added two view with 1px height at top and bottom of view and i added table-view in that custom view.

Upvotes: 1

RajeshKumar R
RajeshKumar R

Reputation: 15748

Try this

    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)

Upvotes: 0

Bhavin Ranpara
Bhavin Ranpara

Reputation: 278

take label in the cell with height value as 1 and give border to it. Programmatically hide/show on your require index.

Upvotes: 0

KAR
KAR

Reputation: 3361

Take two UIView. Give it white color. y cordinate of top uiview is min y of uitableview and y cordinate of bottom uiview is max y of uitableview.

you can get min y and max y using GetMaxY and GetMinY.

width of both uiview are same as uitableview.

height of both uiview are 1px.

hope this will help you.

Upvotes: 0

Jitendra
Jitendra

Reputation: 5081

Use tableview header and footer.

// 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;
});

Same do it for footer also.

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;
    });

Or else just add UIVIEW to the background and set the color. That will also help.

Upvotes: 0

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

If don't want to use the table footer and header you can use the table header view and footer view where you can return view with white color and height would be 1 and width same as table width

Upvotes: 0

Related Questions