swiftui阴影应用于内部元素

Mic*_*air 4 swiftui

我在VStack周围添加了阴影,其中包含两个文本字段和Submit按钮。但是,阴影也将应用于VStack内部的两个文本字段。我是否缺少某些东西导致它发生?我尝试shadow(radius: 0)在文本字段中添加内容,但没有任何改变。如果我从文本字段中删除了填充和背景,则阴影消失了。

    var body: some View {
        VStack() {
            Spacer()

            VStack() {
                TextField($email, placeholder: Text("email"))
                    .padding()
                    .background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))

                SecureField($password, placeholder: Text("password"))
                    .padding()
                    .background(Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255))

                Button(action: { self.login() }, label: { Text("Login").foregroundColor(Color.white) })
                    .padding()
                    .background(Color(red: 0, green: 116 / 255, blue: 217 / 255))
            }
                .padding()
                .background(Color.white)
                .shadow(radius: 10)

            Spacer()
        }
        .padding()
        .background(Color(red: 0, green: 116 / 255, blue: 217 / 255))
        .edgesIgnoringSafeArea(.all)
    }
Run Code Online (Sandbox Code Playgroud)

Fab*_*tel 29

对我来说,它还可以将阴影应用于背景而不是堆栈,例如:

VStack {
  // ...
}.background(Rectangle().fill(Color.white).shadow(radius: 8))
Run Code Online (Sandbox Code Playgroud)

  • 您可以仅使用简单的“Color.white”而不是“Rectangle().fill(Color.white)”来创建彩色矩形 (2认同)

Pra*_*iya 8

您可以clipped()在这里使用以解决此问题

       VStack() {
            Text("Text")
                .background(Color.red) .padding()

                .padding()

            Text("Text")
                .background(Color.purple)
                .padding()

            }
            .padding()
            .background(Color.white)

            .clipped()
            .shadow(color: Color.red, radius: 10, x: 0, y: 0)
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明

希望对您有所帮助:)

  • 即使你没有解释它的作用,这个黑客仍然有效。然而,这并不是苹果给“clipped()”的定义。阴影修改器的继承对我来说仍然像是一个错误。 (5认同)
  • 这对我不起作用。我仍然看到孩子们周围的阴影。 (3认同)
  • @ielyamani 这就是 VStack 和 HStack 的工作原理。如果您向 VStack 提供任何修饰符,它将自动应用于其子项。像 `foregroundColor(Color.red)` 会让 VStack 中的所有 `Text` 变成红色,如果 **Text 没有它自己的 foregroundColor()** (2认同)
  • `.clipped()` 也对我没有帮助,它仍然将 `.shadow` 应用于每个嵌套视图。 (2认同)

pun*_*bit 8

对于正在努力为 进行这项工作的用户RoundedRectangle,请使用:

HStack {
 // Your nested views
}
.overlay(
  RoundedRectangle(cornerRadius: 10)
   .stroke(Color.black.opacity(0.2), lineWidth: 1)
)
.background(
   RoundedRectangle(
     cornerRadius: 10
   )
   .foregroundColor(Color.white)
   .shadow(
     color: Color.black,
     radius: 5,
     x: 0,
     y: 0
   )
)
Run Code Online (Sandbox Code Playgroud)

阴影应用于.background,RoundedRectagle元素shadow

虽然相当冗长,但它很容易阅读,同样的原则可以应用于任何其他形状。


paw*_*222 7

有一个专门的修饰符- compositingGroup

从文档:

/// Wraps this view in a compositing group.
///
/// A compositing group makes compositing effects in this view's ancestor
/// views, such as opacity and the blend mode, take effect before this view
/// is rendered.
/// [...] 
/// This limits the scope of [...] change to the outermost view.
Run Code Online (Sandbox Code Playgroud)
VStack {
    Text("Text")
        .background(Color.red)
        .padding()
        .padding()
    Text("Text")
        .background(Color.purple)
        .padding()
}
.padding()
.background(Color.white)
.compositingGroup()
.shadow(color: Color.red, radius: 10, x: 0, y: 0)
Run Code Online (Sandbox Code Playgroud)

  • 最好的答案是“compositingGroup”按预期工作。虽然“clipped()”对我有用,但它似乎更像是副作用。 (3认同)