I created a subclass of UIButton
which acts as a custom Back button inside UIBarButtonItem
:
- - - - - - - - - - - - - - - - -
| UIBarButtonItem |
| ------------------------ |
/ |
| \ BackButton : UIButton | |
------------------------
| _ _ _ _ _ _ _ _ _ _ _|
As I was switching my code to using UIAppearance
, I noticed that BackButton.Appearance
is, unsuprisingly, inherited from UIButton.Appearance
, and therefore changing it will change all UIButton
s throughout the application and not just my custom buttons.
I know I can use AppearanceWhenContainedIn
but, because UIBarButtonItem
is not an appearance container, I'd have to insert another view between them to recognize back buttons, which I don't want to do.
How do I provide a UIAppearance
proxy for my custom UIButton
subclass that has all the methods available in UIButton.UIButtonAppearance
? I noticed that its constructor is internal.
Adding answer for completeness.
Use
BackButton.GetAppearance<BackButton>()
insteadBackButton.Appearance
.Although there may be a simpler answer, I ended up inheriting from
UIAppearance
directly and copy-pasting relevant piece ofUIButton
andUIButton+UIButtonAppearance
binding implementation from MonoDevelop C# disassembler and fixing internal fields here and there.Inside
BackButton
:Nested
BackButtonAppearance
:This allows me to bring my custom view to support UIAppearance-style API.