Open cfilipov opened 6 years ago
Supporting view controllers might be useful, indeed. It's less clear at which moment they should be styled, though.
Yes, viewDidLoad
might be appropriate, but it would be quite different from UIView
styling where didMoveToWindow
is used (to mimic UIAppearance
), so it happens rather late. viewDidLoad
happens in contrast rather early. It might still be a convenient approach, because with UIKit it's common to do a lot of setup in this method.
I presume you have some experience with your implementation. Does it behave as you (or especially an abstract iOS developer) would expect? Any quirks?
If not, I'd start with integrating view controller support. Please note, that I'd like to support macOS as well (which your current implementation doesn't). Feel free to provide partial implementation, we can collaborate on this.
The reason UIAppearance
injects into didMoveToWindow
is because there isn't any other reliable setup method in UIView
to depend on. You can't be sure that an arbitrary UIView
is setup and ready after init(frame:)
or init(coder:)
, this is why UIAppearance
is implemented the way it is. If UIView
had a viewDidLoad
method then I imagine they would have used that.
So far I haven't had any issues with injecting styles at viewDidLoad
. I use it on every view controller in my app and also on apple's own view controllers and so far so good.
I'll work on getting something working on macOS as well then create a PR for this.
The reason
UIAppearance
injects intodidMoveToWindow
is because there isn't any other reliable setup method inUIView
to depend on. You can't be sure that an arbitraryUIView
is setup and ready afterinit(frame:)
orinit(coder:)
, this is whyUIAppearance
is implemented the way it is. IfUIView
had aviewDidLoad
method then I imagine they would have used that.
This reasoning completely ignores the existence of other methods such as willMove(toSuperview:)
, didMoveToSuperview()
. But this topic doesn't require further discussion, as we have no issues with views.
Looking forward for you pull request.
Good point. I think those methods would also have been good candidates.
It is incredibly useful to have UIViewController styling support and I think this should be included. It's fairly easy to add (see this commit) and it even helps alleviate the problem with styling UITabBarItem as mentioned in #1 since you can do that from within the UIViewController style.