4

I created extension based on this answer: How to add a border just on the top side of a UIView

extension CALayer {

    func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
        //https://stackoverflow.com/questions/17355280/how-to-add-a-border-just-on-the-top-side-of-a-uiview
        let border = CALayer()

        switch edge {
        case UIRectEdge.Top:
            border.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), thickness)
            break
        case UIRectEdge.Bottom:
            border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, CGRectGetWidth(self.frame), thickness)
            break
        case UIRectEdge.Left:
            border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame))
            break
        case UIRectEdge.Right:
            border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame))
            break
        default:
            break
        }

        border.backgroundColor = color.CGColor;

        self.addSublayer(border)
    }
}

But in Ipad i see only 70% border width:

enter image description here

I tried change frame to bounds but it not worked too.

Community
  • 1
  • 1
Arti
  • 7,356
  • 12
  • 57
  • 122
  • The code looks ok to me, 1- are you zooming in or out in simulator? this can crunch some pixels, 2- do you have another object that can have white area that cover that part on the right? – DeyaEldeen Mar 09 '16 at 20:30
  • I tried on real device too, the same. but on iphone ok. – Arti Mar 09 '16 at 20:56

1 Answers1

2

Frame of view is resized after adding border. You add border in viewDidLoad() method right? Try to add border in viewDidAppear() method:

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    yourView.layer.addBorder(UIRectEdge.Top, color: UIColor.redColor(), thickness: 0.5)
}

Move adding layer from

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {}

to

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    cell.layer.addBorder(UIRectEdge.Top, color: UIColor.redColor(), thickness: 0.5)
}
Sasha Kozachuk
  • 1,283
  • 3
  • 14
  • 21
  • I use this method it in table cell too – Arti Mar 09 '16 at 20:57
  • you use it in cellForRow method right? check frame of that cell. Print in console next values: print("cell width = \ (cell.frame.size.width)") and print("view width = \ (view.frame.size.width)") in cellForRow method I think your view is actually wider than cell frame. Better to move method from cellForRow to willDisplayCell method of UITableViewDelegate – Sasha Kozachuk Mar 09 '16 at 21:24
  • @Arti can you please accept as final answer, so I will receive more reputation) i am new here and want to be able to comment, thanks! – Sasha Kozachuk Mar 10 '16 at 16:28