简体   繁体   English

仅在 Swift UI 中将阴影应用于按钮的底部、左侧和右侧部分

[英]Apply Shadow to Button only bottom , left and right part in Swift UI

Below is my code: This code gives shadow to all part of button.下面是我的代码: 此代码为按钮的所有部分提供阴影。 I only want to give shadow to bottom part of button我只想给按钮的底部添加阴影

Button should look like something like this:按钮应如下所示: 在此处输入图像描述

var body: some View {
        Button(action: {
            self.action(self.title)
            self.hover.toggle()
        }) {
            if title == "x" {
                ZStack {
                    Image(systemName: "delete.left").cornerRadius(5).font(Font.system(size: 28, weight: .medium))
                    .background(Color..secondaryBG)
                    .accentColor(.accentTint)
                }
            } else {
                Text(title).customText(titleColor: .accentTint, fontName: .gothamMedium, fontSize: 28)
                    .background(Color.secondaryBG)
            }
        }
        .withBackground(color: .accentTint)
            .frame(width: 76, height: 76)
            .cornerRadius(5)
            .shadow(color: .numberPadShadowColor, radius: 5, x: 0, y: 1) // This gives shadow to all the parts. I do not want to give shadow to top part of button
    }

What you show is more like a drop shadow with hard edges:您显示的更像是带有硬边的阴影:

在此处输入图像描述

give it a solid background and shift it down:给它一个坚实的背景并将其向下移动:

struct ContentView: View {
    var body: some View {
        Button(action: {
            //
        }) {
            Image(systemName: "delete.left")
                .font(Font.system(size: 28, weight: .medium))
                .foregroundColor(.black)
        }
        .frame(width: 76, height: 76)
        .background(Color(uiColor: .lightGray))
        .cornerRadius(5)
        
        .background(
            RoundedRectangle(cornerRadius: 5)
                .offset(x: 0, y: 1)
        )
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM