Welcome to my new series on things I don’t understand about Apple’s premier user interface framework. I’m calling it…
[#]SwiftUWhy
To be clear, these are things I don’t understand, not necessarily things that are “wrong.” They sure look wrong (or at least “suboptimal”) to me! But maybe there are good reasons, and I just don’t know them yet. SwiftUI experts and historians, please tell me!
The first entry is coming up…
=> More informations about this toot | More toots from siracusa@mastodon.social
Why in the world does this interface look like this:
.padding(.top, 1)
.padding(.leading, 2)
.padding(.bottom, 3)
.padding(.trailing, 4)
…or this:
.padding(EdgeInsets(top: 1, leading: 2, bottom: 3, trailing: 4))
…instead of the much more idiomatic:
.padding(top: 1, leading: 2, bottom: 3, trailing: 4)
Like, who in the world thought of this foo(.key, value) API interface in a language with named parameters?!
[#]SwiftUWhy
=> More informations about this toot | More toots from siracusa@mastodon.social
@siracusa I don't like it either. I add this to my projects:
https://gist.github.com/mjmsmith/040ffb4a8536b61bf479ad5b0abeea6e
=> More informations about this toot | More toots from marksmith@mastodon.social
@marksmith @siracusa FWIW, that changes the semantics of nil
from “platform dependent padding” to “no padding” and if you were to animate from nil
to some value, you'd lose the view identity due to the conditional.
Not trying to tell you what to do with your code base, just calling this out for those unaware.
=> More informations about this toot | More toots from dlx@mastodon.social
@dlx @siracusa I could be misunderstanding, but doesn't it have the same behavior as the built-in padding modifier? (In the image, trailing is applied both ways with the same result.)
=> More informations about this toot | More toots from marksmith@mastodon.social
@marksmith @siracusa This only affects the case where you pass in nil, e.g. .padding(.bottom)
which is effectively .padding(.bottom, nil)
.
=> More informations about this toot | More toots from dlx@mastodon.social
@dlx @siracusa Oh yes, you're right. In my modifier, passing nil (implicitly or explicitly) leaves the value alone rather than resetting it to the default.
A bit hacky under the covers, but I could use .nan as the default value in the padding() View extension, and test for .isNaN instead of nil in EdgePaddingModifier. That would make the behaviors match.
=> More informations about this toot | More toots from marksmith@mastodon.social
@marksmith @siracusa yeah, if you think that's worth it.
=> More informations about this toot | More toots from dlx@mastodon.social
@dlx @marksmith @siracusa Should be easy enough to fix the animations by summing the values into a single EdgeInsets to remove the conditional too :)
=> More informations about this toot | More toots from digitalfx@mastodon.social
@dlx @marksmith @siracusa Ah, although that would then conflict with the .nan default 😕
=> More informations about this toot | More toots from digitalfx@mastodon.social
@digitalfx @dlx @siracusa It's enough to make you think Apple had reasons for doing it the way they did in the first place. :)
=> More informations about this toot | More toots from marksmith@mastodon.social
@marksmith It’s not! There’s no reason to bend over backwards to match the weird functionality of an existing API when adding new, more idiomatic versions. The weird versions still exist for the people who want them!
=> More informations about this toot | More toots from siracusa@mastodon.social
text/gemini
This content has been proxied by September (3851b).