0

I've added a functionality to UIView by extending drawRect. This works great for a simple UIView, but the problem is that I want it to work also for UILabel, UIButton, UIImageView and every other component that inherits from UIView.

I was wondering what is the best way to achieve this. Can I make a subclass of UILabel (for example) that inherits from MyView instead of UIView?

adamsiton
  • 3,642
  • 32
  • 34

1 Answers1

-1

You can make a category of UIView and override drawRect function, so all subclasses of UIView, that call to [super drawRect:...] will use this category function, though you should be extremely cautious while doing this, since you override something very basic there. I did it at time with UINavigationBar when I would need to add a background picture there. But take into consideration that not all subclasses of UIView call [super drawRect:...].

Nava Carmon
  • 4,523
  • 3
  • 40
  • 74
  • 1
    a category allows me only to override an implementation, not to extend it, meaning that I can't call [super drawRect:] inside my overridden method. I need to extend the basic functionality of drawRect, not override it. – adamsiton Jan 26 '12 at 15:17
  • According to WWDC 2011 session 123 "Improving The Stability of Your App" - you should never over-ride drewRect. The recommanded practice by Apple is to extend the UIView class – Oded Regev Jan 26 '12 at 15:25
  • see if this helps: http://stackoverflow.com/questions/1405060/using-super-in-an-objective-c-category – Nava Carmon Jan 26 '12 at 15:27
  • @NavaCarmon, thanks, I read this question already but I think that method swizzling is such a hugh overkill for this case. Also, I don't really know how method swizzling affect performance, or whether or not apple even approve apps that use it. I hope there's a simpler way to do what I need. – adamsiton Jan 26 '12 at 15:33