代码之家  ›  专栏  ›  技术社区  ›  swiftPunk

如何在SwiftUI中将自定义形状限制在安全区域?

  •  0
  • swiftPunk  · 技术社区  · 4 年前

    我有两个形状 自定义形状1 仅限于安全区域,可以,但是 自定义形状2 是忽略保险箱区域开箱即用,我对此不满意。在 自定义形状1 我可以通过原点和大小将我的形状可用空间限制为安全区域,但我没有或看不到通过原点或大小的方法 自定义形状2 这只是一个圆圈,所以我该如何解决这个问题,那个 自定义形状2 尊重保险箱右侧的区域?

    我发现添加填充修改器不会以我想要的方式解决问题,我不想使用视图修改器来解决形状问题。正如您所看到的添加 .padding(.vertical, 0.1) 会将视图限制在safeArea,我知道我应该称之为bug还是什么!

    struct ContentView: View {
        var body: some View {
    
            ZStack {
                
                CustomShape1()
                    .fill(Color.red)
                
                CustomShape2()
                    .fill(Color.blue)
                    .background(Color.black.opacity(0.8))
                    //.padding(.vertical, 0.1) // Adding This Would solve the issue!
                
            }
    
        }
    }
    
    
    struct CustomShape1: Shape {
        func path(in rect: CGRect) -> Path {
            Path { path in
                path.addRect(CGRect(origin: rect.origin, size: rect.size))
            }
        }
    }
    
    
    struct CustomShape2: Shape {
        func path(in rect: CGRect) -> Path {
            Path { path in
                path.addArc(center: CGPoint(x: rect.midX, y: rect.midY), radius: 50, startAngle: Angle(degrees: 0), endAngle: Angle(degrees: 360), clockwise: true)
            }
        }
    }
    

    enter image description here

    1 回复  |  直到 4 年前
        1
  •  1
  •   Raja Kishan    4 年前

    设置 .clipped()

    .background(Color.blue.opacity(0.8).clipped()) // <===Here