SwiftUI中的布局与对齐指南

本文介绍了SwiftUI中的布局和对齐指南,通过示例解释了如何使用ZStack, VStack, HStack等容器来管理空间。文章指出,SwiftUI的布局语义与UIKit中的约束类似,但默认情况下布局指南可能不生效。通过创建多个框架并调整对齐指南,可以实现所需布局效果。" 105397309,9379562,测量平差理论与应用,"['测量技术', '数学应用', '平差理论', '数据处理', '工程测量']
摘要由CSDN通过智能技术生成

If I had to name an aspect of SwiftUI development that initially confounded me, it would be alignment guides. As such, I decided to write this article to try to demystify them a little, map out how you can work with them, and indeed make them work for you.

如果我不得不说起最初使我困惑的SwiftUI开发的一个方面,那就是对齐指南。 因此,我决定写这篇文章,试图使它们变得神秘,并阐明如何与他们合作,并确实使它们为您服务。

Alignment guides in SwiftUI are like constraints in UIKit. No, I take that back. They are constraints in SwiftUI — only instead of micromanaging your screen, they macromanage it. You need macro-management for screens these days because they come in all shapes and sizes. However, much like constraints, alignment guides aren’t always that easy to use.

SwiftUI中的对齐指南类似于UIKit中的约束。 不,我拿回去。 它们是SwiftUI中的约束-只是对屏幕进行宏观管理,而不是对其进行微观管理。 如今,您需要对屏幕进行宏管理,因为它们具有各种形状和大小。 但是,就像约束一样,对齐指南并不总是那么容易使用。

The first thing you need to appreciate is the semantics behind the layout mechanism in SwiftUI. The best place to learn about that is Dave Abrahams and John Harper’s talk at 2019 WWDC on the subject. In it, they outline three golden rules for layout:

您需要了解的第一件事是SwiftUI中的布局机制背后的语义。 最好的学习方法是Dave Abrahams和John Harper在2019年WWDC上有关该主题的演讲 。 在其中,他们概述了布局的三个黄金法则:

  • Parent proposes a size for the child.

    父母为孩子建议一个尺寸。
  • Child uses proposal to determine its own size.

    儿童使用提案确定自己的尺寸。
  • Parent uses that size chosen by the child to position said child.

    父母使用孩子选择的尺寸放置孩子。

“What does this have to do with alignment guides?” you may ask. Absolutely everything. To lay out/manage space in SwiftUI, you use containers (ZStack, VStack, and HStack). Containers that are the parents in the semantics above, containers that will organize their siblings using said rules. You can influence these rules with your alignment guides. But wait, there is a snag. Normally, the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值