16

How can I set right, left, top and bottom border with color on UITableview in swift?

Thanks,

3 Answers 3

46

Try this for full border:

yourtable.layer.masksToBounds = true yourtable.layer.borderColor = UIColor( red: 153/255, green: 153/255, blue:0/255, alpha: 1.0 ).CGColor yourtable.layer.borderWidth = 2.0 

This is for bottom border:

let border = CALayer() let width = CGFloat(2.0) border.borderColor = UIColor.darkGrayColor().CGColor border.frame = CGRect(x: 0, y: yourtable.frame.size.height - width, width: yourtable.frame.size.width, height: yourtable.frame.size.height) border.borderWidth = width yourtable.layer.addSublayer(border) yourtable.layer.masksToBounds = true 
Sign up to request clarification or add additional context in comments.

6 Comments

thanks... it worked!! One more question can we have auto height of the Uitableview ..so that its height increase or decreasing depeneds on data comes in ?(sorry it is not related to the same subject)
thank you very much! it worked and I saved a lot of research time ;-)
@AlexWoe89 Welcome anytime :)
that's great! Worked
this is beautiful, thank you. Works with Swift 3, too!
|
15
extension UIView { func addBorderTop(size size: CGFloat, color: UIColor) { addBorderUtility(x: 0, y: 0, width: frame.width, height: size, color: color) } func addBorderBottom(size size: CGFloat, color: UIColor) { addBorderUtility(x: 0, y: frame.height - size, width: frame.width, height: size, color: color) } func addBorderLeft(size size: CGFloat, color: UIColor) { addBorderUtility(x: 0, y: 0, width: size, height: frame.height, color: color) } func addBorderRight(size size: CGFloat, color: UIColor) { addBorderUtility(x: frame.width - size, y: 0, width: size, height: frame.height, color: color) } private func addBorderUtility(x x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: UIColor) { let border = CALayer() border.backgroundColor = color.CGColor border.frame = CGRect(x: x, y: y, width: width, height: height) layer.addSublayer(border) } } 

I am going to open source my extension classes at some point.

Edit: Here you go, I update the functions in here https://github.com/goktugyil/EZSwiftExtensions

5 Comments

I sure hope you'll factor out all of those copied & pasted statements into a utility method.
All four methods are identical except for the size of the frame. Figure out how to write a utility method that takes a CGRect argument for the border's frame, then use it to create the border layer and add it as a sublayer.
I've tried using it, but the border line is sticking out to the right, for the addBorderTop
thats weird, how much px does it stick out?
I found that I add to place this code in viewDidAppear before it would work. viewDidLoad did not work.
4

if you want to give the border to tableview with color use below code for swift 3 :

yourTableView.layer.borderColor = UIColor.gray.cgColor yourTableView.layer.borderWidth = 1.0 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.