One additional slider appear when Inheriting from UISlider

508 views Asked by At

I recently purchased paintcode. I used this to create a slider. In Xcode, when I, In my storyboard create an empty view and set it to a class Slider.m where my slider code is, and run this app, this happens:

enter image description here

My drawing code is inside -

(void)drawRect:(CGRect)rect {}

My Slider.h is inheriting fram UISlider. If I inherits from say UIButton, only my drawing appears.

Why is that?

1

There are 1 answers

0
Till On BEST ANSWER

The displayed results are reasoned in the fact that UISlider (among many other controls) in fact is composed of multiple subclasses which handle the drawing themselves. The results is a highly optimized display code which can not properly be overridden using drawRect: on the UISlider only. You would have to override several other, partially closed implementations of that composed control.

In fact, there is a much easier and much more performant solution which is providing the graphical elements you wish to customize to UISlider. A nice introduction on that subject can be found on Apple's conceptual introduction to Sliders.

There some smaller pitfalls though - one of them is the fact that a UISlider's thumb does not "like" to be replaced by something with a different size. Those are however covered by many tutorials - one of those would be how-to-make-a-custom-uislider.

As a rule of thumb: avoid subclassing and overriding drawRect: when possible by any means as it will vastly degrade the performance of your UI.

Instead look at solutions that make use of the UIAppearance protocol for controls that do not provide interfaces for customizing. UISlider however does provide them, so go for those.

This would be my approach priority list:

  1. Check very carefully if the control in question does allow customizing by providing images etc.
  2. Check if the control in question adheres to the UIAppearance protocol.
  3. Use subclassing and possibly overriding drawRect:
  4. When it turns out that the control in question appears to handle drawRect: in a "weird" way, subclass UIView and recreate the missing logic yourself.