How to add a custom UIControl class in storyboard?
10,251
You are doing it correctly -- drop a UIView
onto your storyboard and assign the class in the Identity Inspector. Don't forget to override initWithCoder:aDecoder
in the custom class.
Related videos on Youtube
Author by
3254523
Updated on June 24, 2022Comments
-
3254523 about 2 years
I have a custom UIControl class that I want to use in my storyboard. I am having problems since there isn't a UIControl in the object library in the Storyboard console.
I tried adding a UIView and assigning my custom UIControl class to it, but it seems to not register the events that I need for the control to use(Value Changed, Touch Drag Inside, Touch Up Inside).
When debugging, I breakpointed the actions related to the events and it looks like they are not even called.
How can I use my custom UIControl class correctly in my storyboard?
-
3254523 almost 10 yearshey thanks for responding. just making sure that i'm doing this right can you confirm i'm doing this step right? - go to Identity Inspector and change the class to my custom UIControl Class. And after that... What would I be doing in
initWithDecoder:aDecoder
to point to that storyboard object? -
3254523 almost 10 yearsI've done something similar when dealing with a .xib file, but since this is on storyboard, i'm not sure how to load the specific view, and if theres any kind of ID i need to assign it within storyboard
-
memmons almost 10 years@christopher.ryan.cruz In
initWithDecoder
you'd be doing anything you'd normally do ininitWithFrame
sinceinitWithFrame
won't be called on a view dropped into a storyboard. -
memmons almost 10 yearsI'm not sure what you mean by "load the specific view". The view will be loaded automatically as part of the storyboard unarchiving.
-
Fattie about 8 yearsThanks for the great answer. Note, you don't, absolutely, nor necessarily, have to use
initWithDecoder
. Firstly, you don't necessarily need any particularly setup; don't use it if not needed. Secondly the pairrequired init?(coder decoder: NSCoder)
/override init(frame:CGRect)
is useful in Swift. ThirdlyawakeFromNib
can be a good choice (assuming you use storyboard normally). Unfortunately IBDesignable becomes involved; with custom controls you always use that. it's a tricky issue! Some discussion: stackoverflow.com/a/37444957/294884