Swift UI Button and focus ring

I try to remove the focus ring which appear on my buttons in one of my control view, unsuccessfuly ...


I tried a lot of things, listed all the available mlodifiers, but no luck !


I don't want to fall back to an AppKit view in SwiftUI


Any clues ?

Well my own answer, can be useful for others.


I hesitated to use my own button style ... but this is the solution here in my case


struct SimpleButtonStyle: ButtonStyle {
  func pressColor(isPressed: Bool) -> Color{
      if isPressed {
        return Color.gray
      }
      else {
        return Color.white
      }
  }
 
  func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
      .padding([.trailing, .leading], 10)
      .padding([.top, .bottom], 1)
      .background(
        RoundedRectangle(cornerRadius: 5)
          .fill(pressColor(isPressed: configuration.isPressed))
          .overlay(RoundedRectangle(cornerRadius: 5)
            .stroke(lineWidth: 1)
            .foregroundColor(Color.gray)
        )
    )
  }
}

struct SomeButton : View {
  
  var body: some View {
    HStack{
      Button(action: {
        print("Pressed")
      }) {
        Text("Press")
      }
    }
    .buttonStyle(SimpleButtonStyle())
    .font(.headline)
    .padding(.trailing, 10)
  }
}


Take care

Thanks for feedback. Don't forget to close the thread.


In UIKit, you have to makeFirstResponder another object.


Hope that can give a hint, but I've not looked at this in SwiftUI.

Swift UI Button and focus ring
 
 
Q