3

I am subclassing UIStepper and I'd like to apply some custom styling. I am doing using following method…

func setupStepper() {
    let incrementImageFromFile : UIImage = UIImage(named: "plusSymbol")!
    let incrementImage : UIImage = incrementImageFromFile.withRenderingMode(UIImageRenderingMode.alwaysOriginal)

    let decrementImageFromFile : UIImage = UIImage(named: "minusSymbol")!
    let decrementImage : UIImage = decrementImageFromFile.withRenderingMode(UIImageRenderingMode.alwaysOriginal)

    tintColor = UIColor.clear()
    setDecrementImage(decrementImage, for:  [])
    setIncrementImage(incrementImage, for:  [])
}

I tried hooking this styling into the following methods but it doesn't get called.

class RoundedStepper: UIStepper {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupStepper()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupStepper()
    }

    override func awakeFromNib() {
        setupStepper()
    }

    …

I am using this custom stepper subclass in a Storyboard. Why doesn't setupStepper() get called? Where is the correct place to add this styling method?

Bernd
  • 11,133
  • 11
  • 65
  • 98
  • did you forget to set Class field in storyboard `UIStepper` view to `RoundedStepper`? Also you should call `super.awakeFromNib()` in `awakeFromNib` – Yury Jun 28 '16 at 14:07
  • Thanks for the note. But no, I did that. Just to double check I added it to `override func layoutSubviews() {…}` and it did get called there. But this feels like it's the wrong place for this customisation code. – Bernd Jun 28 '16 at 14:11
  • If Class set properly, `awakeFromNib` must be called while view initializing from storyboard, and this is right place to call customization function. – Yury Jun 28 '16 at 14:14
  • Have you added breakpoints to make sure neither init with frame or init with coder are being called? They're the only 2 entry points for UIStepper that I could find. – Luke Jun 28 '16 at 15:06
  • Oh, yes. I tried again. And now both `init?(coder aDecoder: NSCoder)` and `awakeFromNib()` get called! To be honest I have no idea why this didn't work before. Thank you. – Bernd Jun 28 '16 at 15:25
  • …maybe changing the Storyboard required a clean build? Hm. – Bernd Jun 28 '16 at 15:25

0 Answers0