resize image from UIImage inside UIButton with swiftUI

Hello,

I'm having a little issue with this code.
Code Block Swift
struct nfcButton : UIViewRepresentable {
    @Binding var data: String
    @Binding var showData: Bool
    func makeUIView(context: UIViewRepresentableContext<nfcButton>) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: "phoneFinal"), for: .normal)
        button.addTarget(context.coordinator, action: #selector(context.coordinator.beginScan(_:)), for: .touchUpInside)
        return button
    }


I just change my image with a better and bigger one.
The thing is don't know how to resize it.
  • I tried to add .frame to the structure call like so :

Code Block Swift
nfcButton(data: $data, showData: $showNFCMobile)
.frame(width: 150.0, height: 250.0)
  • I also tried with resizableImage method but I don't really understand what is the capInsets

Code Block Swift
button.setImage(UIImage(named: "phoneFinal")?.resizableImage(withCapInsets: <#T##UIEdgeInsets#>), for: .normal)


I know in my regular view I just need to make like this and it works;

Code Block Swift
Image("Image_name").resizable().frame(width: 150)


Thank for your help !

Replies

You can try this:

Button {
               // Button Action here

                    } label: {
                      Image(systemName: "arrowtriangle.right").resizable()
                     .foregroundColor(Color("Feature Green"))
                     .frame(width: 27, height: 27)
                     }