E
E
Emil2021-05-23 22:02:41
Swift
Emil, 2021-05-23 22:02:41

How to "read"/"understand" the SwiftUI documentation?

Hello everyone, reading the documentation for the same C #, everything is extremely simple and clear. This is responsible for this, this is responsible for that. But I just can't figure out how to take the Apple documentation? For example, I'm currently studying SwiftUI, trying to make a test-training project. The question arose of how to position a specific View, from the very bottom. It is clear that this can be done "programmatically"

BottomMenu()
                .offset(CGSize(width: 0, height: screenHeight / 2 - 40))

And get:
60aaa5f1cea4d243631986.png
But I think this is not quite the right approach, if in the same UIKit, you can just scatter the constraints, then there are problems with this.
I found anchorPreference , I open the documentation and see this 60aaa693408b7122414242.png:
60aaa69a73ddf571567518.png
60aaa6a02b1cf083720758.png
I perfectly understand that neither Apple is stupid, they write whatever, but I can’t understand.
But how to read it, how to understand what for what?
I ask all who read, be indulgent to my stupidity, and please explain.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
briahas, 2021-05-23
@briahas

Well, I won’t say how often (but enough to notice) in the Appl documentation there are such empty articles where you can’t figure it out without stackowerflow. So don't talk to yourself.
To make anchorPreference work, you need to implement a custom class (struct) that satisfies the PreferenceKey protocol . (...some View where K : PreferenceKey).

  • The type of this custom class ( MyCustomKey.self ) will be passed to key .
  • In value , pass a value of type Anchor.Source (click on Source and see the options)
  • In transform there is a clause that returns an object of type PreferenceKey.Value (have you already implemented the PreferenceKey protocol?), along the way, this will be another custom class of yours. In fact, just initiating an instance of this type in a clojure and it returns it, like:
    .anchorPreference(..., transform: { MyPreferenceKeyValue(myVariableToStoreAnchor: $0) })


(all the names of classes and variables are taken from the bullshit and do not claim to be significant)
What you have in the second picture is what comes after the words "See Also" ... you need to look there (IMHO) well, very rarely ... Usually it is enough what is written above this phrase.
As I said - "you can't figure it out without stackowerflow."

R
Roman Kerimov, 2021-10-14
@RomanKerimov

VStack {
    Spacer()
    BottomMenu()
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question