Okay, so, I’ve been messing around with my iPhone, trying to get those rounded corners on some elements, and I ran into a couple of weird situations. I want to share my little journey here, and hopefully, it helps someone out there.
The Initial Setup
I started simple. I had this UIView
, let’s call it myView
, and I wanted to give it rounded corners. The obvious thing to do, and what I did, was this:
* = 10.0
* = true
Boom! Rounded corners, right? Most of the time, yeah. But then things got… interesting.
The First “Aha!” Moment
I was adding this myView
as a subview to a container view, and the container view also had rounded corners. Guess what? My inner view’s rounded corners were getting clipped! It looked, frankly, awful. It looked a bit squared.
I banged my head against the wall for a bit. I checked, double-checked, and triple-checked the cornerRadius
and masksToBounds
settings. Everything seemed right.
Then, I realized something. It was the order of operations. I was setting the corner radius of the container view after adding myView
as a subview. I switched things around:
- First, I set the corner radius on the container view.
- Then, I added
myView
as a subview. - Finally, I set the corner radius on
myView
.
It worked! The inner view’s rounded corners were now showing correctly, nestled nicely within the rounded corners of the container.
The Shadow Situation
Next challenge! I needed to add a shadow to myView. I am not a fan of shadows but I had to.
I add the below lines of code:
* = *
* = CGSize(width: 0, height: 2)
* = 4.0
* = 0.5
The shadow went all around myView. After a few tries, I remembered that the shadow is also clipped by corner radius.
So, the shadow has to be on a separate view.
I created a new view called shadowView.
- I set the corner radius of shadowView to be equal to myView’s corner radius.
- I added myView to shadowView.
- Then, I added the shadowView to the main container view.
And that, my friends, is how I spent a good chunk of my day wrestling with rounded corners and shadows on an iPhone. It’s the little things, you know? Hopefully, my little adventure saves someone else some time and frustration. It’s always something with these corner cases!