一. 声明:
![](https://img.haomeiwen.com/i961759/0d52fcb3ed5c63e6.png)
1. padding() -> View
padding()
无参数时,默认对上下左右都有一定距离。
2. padding(_ insets: EdgeInsets) -> View
对上下左右每个方向距离都进行设置:
.padding(EdgeInsets(top: 10, leading: 20, bottom: 30, trailing: 40))
3. padding(_ length: CGFloat) -> View
对四个方向距离都进行统一间距的设置。如下代码,上下左右都有20像素的距离。
.padding(20) 距离四个方向20像素
4. padding(_ edges: Edge.Set, _ length: CGFloat?) -> View
对具体某个方向的距离设置。
top
: 上
leading
: 左
bottom
: 下
trailing
: 右
all
:四个方向,不写参数时默认为all
。
horizontal
: 左+右
vertica
:上+下
.padding(.top, 10) 距离顶部10像素
二. 示例
- 无padding时。
VStack {
Text("11111111111111")
.border(Color.green)
}.border(Color.red)
![](https://img.haomeiwen.com/i961759/a4c856111dae761c.png)
2.padding()无参数时,上下左右都有个默认距离。
VStack {
Text("11111111111111")
.border(Color.green)
.padding()
}.border(Color.red)
![](https://img.haomeiwen.com/i961759/0ebb340a8e03cf19.png)
- padding()参数为距离上下左右的间距。
VStack {
Text("11111111111111")
.border(Color.green)
.padding(.top, 10) /*距离顶部10像素
.padding(.leading, 10) /*距离左侧10像素
}.border(Color.red)
或者
VStack {
Text("11111111111111")
.border(Color.green)
.padding([.top, .leading], 10)
}.border(Color.red)
![](https://img.haomeiwen.com/i961759/35805bc1a7da984b.png)
网友评论