In my iPad app, I want a second view to appear within the main when the user clicks a button. The new view will be smaller than the first, and darken the background when it is displayed. I want the top two corners of the new view to appear rounded, but using cornerRadius sets all of them rounded. How can I make just two corners rounded?
Share
You have to do this in drawRect:. I actually modified the classic addRoundedRectToPath: so that it takes a bitmap and rounds the corners you request:
This takes a bitmask (I called it UIImageRoundedCorner because I was doing this for images, but you can call it whatever) and then builds up a path based on the corners you want rounded. Then you apply that path to the view in drawRect:
As I said, I was doing this for UIImages, so my code isn’t exactly set up for use in drawRect:, but it should be pretty easy to adapt it. You’re basically just building up a path and then clipping the context to it.
Edit: To explain the bitmask part, it’s just an enum:
In this way you can OR things together to form a bitmask that identifies corners, since each member of the enum represents a different power of two in the bitmask.
Much Later Edit:
I’ve discovered an easier way to do this using
UIBezierPath‘sbezierPathWithRoundedRect:byRoundingCorners:cornerRadii:. Just use the bezier path object’sCGPathin your context.