drawRect on top of subviews

A subview will always be drawn on “top” of its superview. Depending on your specific requirements, you may need to have a plain UIView as the background/container view, with your existing subviews in there, and then your custom view as another subview added to the very top, so it has the highest Z-order. This would … Read more

How to fill a path with gradient in drawRect:?

I would clip to the path you want to fill, and use CGContextDrawLinearGradient. Here is a simple implementation of drawRect: as an example: – (void) drawRect:(CGRect)rect { // Create a gradient from white to red CGFloat colors [] = { 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0 }; CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB(); CGGradientRef gradient … Read more

UIView with shadow, rounded corners and custom drawRect

This is a tricky one. UIView‘s clipsToBounds is necessary to get the rounded corners. But CALayer‘s masksToBounds has to be false so the shadow is visible. Somehow, everything works if drawRect is not overridden, but actually it shouldn’t. The solution is to create a superview to provide the shadow (in the demonstration below this is … Read more