What is Geometry Reader in SwiftUI?

后端 未结 2 550
小鲜肉
小鲜肉 2020-11-28 05:41

I am learning SwiftUI. And I come across to \"GeometryReader\". And I want to know why and when to use it?

2条回答
  •  盖世英雄少女心
    2020-11-28 06:05

    What is called Reader in The SwiftUI?

    In addition to the kontiki's answer, Readers are container views that define their content as a function. So they can have some access and abilities about their parent. They are generic structs if you look more closely and there is 2 readers available now in SwiftUI 2.0:

    Note that it's just a convention, they're not conforming special protocl more of the View protocl.

    GeometryReader

    struct GeometryReader : View
    

    This is A container view that defines its content as a function of its own size and coordinate space. So you can detect frame and position changes and the current state of any view withing a GeometryReader. One of the popular usage of this reader that when you need separate views in separate stacks have the same (or relative) sizes.


    ScrollViewReader

    struct ScrollViewReader : View
    

    This is view whose child is defined as a function of a ScrollViewProxy targeting the scrollable views within the child. So you can have some access to the scrollview like scrolling to a specific item in a list or similar stuff.

    To minimalizing the duplication, I didn't post examples, you can check the link for more information if you want

提交回复
热议问题