Create custom highlight component (Like subclassing UIControl)

Updated
Sep 19, 2019 2:00 PM
Created
Sep 15, 2019 3:19 PM
Tags
SwiftUIMemo
Keywords
Date

Implementation

struct OverlayButton<Content: View>: View {
  
  private let content: Content
  
  init(
    @ViewBuilder _ content: () -> Content
  ) {
    self.content = content()
  }
  
  var body: some View {
    Button(action: {}) { content }
      .buttonStyle(_ButtonStyle())    
  }
  
  private struct _ButtonStyle: ButtonStyle {
    
    func makeBody(configuration: Self.Configuration) -> AnyView {
      if configuration.isPressed {
        return AnyView(
          configuration.label
            .background(Color(white: 0.96))
        )
      } else {
        return AnyView(
          configuration.label
            .background(Color(white: 1, opacity: 0.0001))
        )
      }
    }
  }
  
}

Sample Usage

private func row(title: String, onTap: @escaping () -> Void) -> IdentifiedView {
  
  IdentifiedView {
    OverlayButton {
      HStack() {
        Text(title)
          .multilineTextAlignment(.leading)
          .font(.system(.body, design: .default))
          .foregroundColor(.black)
        Spacer()
      }
      .padding(.init(top: 16, leading: 0, bottom: 16, trailing: 0))
    }
  }

}