Is it possible to make a triangle UIView?

I want to display the shape of the UIView of the triangle, instead of using the regular UIView. Is it possible?

+5
source share
1 answer

UIView is always a rectangular shape, you cannot change this. However, you can get the effect you want using CALayer masking. Create a UIView and apply a custom mask to it, in which the mask has the corresponding data for the triangle. Then any actual content that you put in your UIView will be displayed only in the corresponding area of ​​the "triangle".

To make a mask layer, you can use an image (for example, png) or use kernel graphics to draw a triangle.

There are tons of information about such things in Apple Quartz 2D docs .

Here is an example:

http://cocoadev.com/MaskingACALayer

See also this question:

Basic Graphics Training

There is also information about this in the Apple API docs for UIView and CALayer.

+11
source

All Articles