2 Star 23 Fork 10

小弟调调/swiftui-example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
.github
.idoc
assets
example
accessibility
advanced-state
alerts-and-menus
animation
appendix-a
composing-views
demo1
demo2
demo3
Demo
README.md
demo4
demo5
demo6
demo7
containers
cross-platform
data
drawing
forms
images-shapes-media
introduction
lists
presenting-views
quick-start
responding-to-events
stacks-grids-scrollviews
taps-and-gestures
tooling
transforming-views
user-interface-controls
view-layout
working-with-static-text
cheat-sheet.md
.gitattributes
.gitignore
LICENSE
README.md
idoc.chapters.yml
idoc.yml
package.json
renovate.json
克隆/下载
README.md 1.18 KB
一键复制 编辑 原始数据 按行查看 历史

如何将视图存储为属性?

如果您在另一个视图中嵌套了几种视图,则可能会发现为某些或全部创建属性,以使布局代码更容易。 然后,您可以在视图代码内彻底引用这些属性,帮助保持清除。

例如,这会创建两个文本视图作为属性,然后将它们放在 VStack 中:

struct ContentView: View {
    let title = Text("Paul Hudson")
                    .bold()
    let subtitle = Text("Author")
                    .foregroundColor(.secondary)

    var body: some View {
        VStack {
            title
            subtitle
        }
    }
}

如您所见,只需在堆栈中写入属性名称就足够了。

但是,甚至更好就是您可以将修改器附加到这些属性名称,如下所示:

struct ContentView: View {
    let title = Text("Paul Hudson")
                    .bold()
    let subtitle = Text("Author")
                    .foregroundColor(.secondary)

    var body: some View {
        VStack {
            title
                .foregroundColor(.red)
            subtitle
        }
    }
}

这不会改变标题的底层样式,只有它的一个具体用法。

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Swift
1
https://gitee.com/jaywcjlove/swiftui-example.git
git@gitee.com:jaywcjlove/swiftui-example.git
jaywcjlove
swiftui-example
swiftui-example
main

搜索帮助