Arti
Arti

Reputation: 7772

Add border for view not working for ipad

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.

Upvotes: 4

Views: 694

Answers (1)

Sasha Kozachuk
Sasha Kozachuk

Reputation: 1303

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

Upvotes: 2

Related Questions