How to align the an image on top of a button in swiftui?

后端 未结 1 2082
长发绾君心
长发绾君心 2021-01-26 20:40

I wish to add a \'trash\' image on the top-right side of each button when \'Delete Button\' is pressed, so that when user hits the trash image, the button will be removed from t

相关标签:
1条回答
  • 2021-01-26 20:56

    Here is a demo of possible approach. Tested with Xcode 11.4 / iOS 13.4 (with some replicated code)

    var body: some View {
           Button(action: { }) {
            Text("Name")
           }
           .buttonStyle(GradientBackgroundStyle(isTapped: tapped))
           .overlay(Group {
                if self.isEdit {
                    ZStack {
                        Button(action: {print(">> Trash Tapped")}) {
                           Image(systemName: "trash")
                                .foregroundColor(.red).font(.title)
                        }.padding(.trailing, 40)
                        .alignmentGuide(.top) { $0[.bottom] }
                    }.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
    
                }
           })
           .padding(.bottom, 20)
    }
    
    0 讨论(0)
提交回复
热议问题