I am trying to create a custom UIVIew-based display box for my app in the form of a "badge" with a title "My name is..." and an empty gray area underneath. I am using Swift with UIKit to draw out the badge which is made up of a UIView with gray background and a UILabel with white text & blue background as a subview of the UIView - here is a screenshot from Xcode:
If I run this in Xcode, the basic "badge" looks like this:
The next thing I want to do is add rounded edges to the UIView with the following code in viewDidLoad() which results in the following image:
// Add rounded edges to all four corners
badgeView.layer.cornerRadius = 20
badgeView.clipsToBounds = true
If I then comment out the above code, and can then add the below code then I am able to add shadows to my badge successfully:
// Apply shadow
badgeView.layer.shadowColor = UIColor.black.cgColor
badgeView.layer.shadowRadius = 5.0
badgeView.layer.shadowOpacity = 1.0
badgeView.layer.shadowOffset = CGSize(width: 1, height: 1)
badgeView.layer.masksToBounds = false
However, when I try to use code to do both - have rounded edges and shadow at the same time - I get the following incorrect result:
The problem I am facing is that the top-left and top-right corners are not rounded. Only the bottom-left and bottom-right corners are rounded. I am able to have rounded corners when there is no shadows, and I am able to have shadows when there is no rounded corners. But I want to have both enabled concurrently so that my "badge" has rounded corners and shadows.
How can I solve this problem?