0

How can I let the only two bottom corners to be rounded in Swift?

let maskPathTop = UIBezierPath(roundedRect: tableView.bounds, byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 5, height: 5.0))
let shapeLayerTop = CAShapeLayer()
shapeLayerTop.frame = tableView.bounds
shapeLayerTop.path = maskPathTop.CGPath
tableView.layer.mask = shapeLayerTop

I have tried this code but it didn't work. Any help would be appreciated.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
henry
  • 83
  • 1
  • 13
  • 4
    Possible duplicate of [Only bottom corners rounded](http://stackoverflow.com/questions/36646115/only-bottom-corners-rounded) – Ahmad F Dec 28 '16 at 07:19
  • i tried that code but didn't work – henry Dec 28 '16 at 07:22
  • Possible duplicate of [how to set cornerRadius for only bottom-left,bottom-right and top-left corner textview](http://stackoverflow.com/q/31232689/5790492), [how to set cornerRadius for only top-left and top-right corner of a UIView?](http://stackoverflow.com/questions/10167266/how-to-set-cornerradius-for-only-top-left-and-top-right-corner-of-a-uiview) – Nike Kov Dec 28 '16 at 07:24
  • I suggest to edit your question and mention that the answer of this question didn't work for you. – Ahmad F Dec 28 '16 at 07:28

2 Answers2

0

When i met task like yours, i tried a lot of solutions with code, but the fastest and easiest was to make UIView with all corners rounded and attach it to table view in the bottom. And make the background colors same. So rounded corners will be only at UIView. And it will be at the front layer.

You can create UIView with 5 height, for example, and attach it at the bottom of UITableView.

This answer will not help if you have specific UI design.

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
0

All you need is to construct a bezierPath using the following methods:

  1. https://developer.apple.com/reference/uikit/uibezierpath/1624343-move
  2. https://developer.apple.com/reference/uikit/uibezierpath/1624354-addline
  3. https://developer.apple.com/reference/uikit/uibezierpath/1624367-addarc

Translate degrees angles to radians:

#define degreesToRadians(x) ((x) * M_PI / 180.0)

You will need 4 points:

  1. top left
  2. top right
  3. bottom right centre (width - cornerRadius, height - cornerRadius)
  4. bottom left centre (cornerRadius, height - cornerRadius)

Set fillColor and strokeColor to yours CAShapeLayer object. And don't forget to update layers frame on layoutSubviews method

I wish it would help

Dmitriy
  • 81
  • 2
  • 10