1

I'm trying to resize a SF Symbol button in SwiftUI. I've tried .font(.system(size: 60)) and .imageScale(.medium) but it doesn't work. It still has this same error. This is my code:

var buttons: [Button<Image>] = [
    Button(action: {
        print("Add tapped")
    }) {
        Image(systemName: "plus.circle.fill")
    }
]

Cannot convert value of type 'some View' to closure result type 'Image'

aheze
  • 24,434
  • 8
  • 68
  • 125

2 Answers2

4

Take the help of UIImage.

var buttons: [Button<Image>] = [
    Button(action: {
        print("Add tapped")
    }) {
        let config = UIImage.SymbolConfiguration(font: UIFont.systemFont(ofSize: 60))
        let image = UIImage(systemName: "plus.circle.fill", withConfiguration: config) ?? UIImage()
        return Image(uiImage: image)
    }
]

But the question is still why you need this [Button<Image>] ???

Raja Kishan
  • 16,767
  • 2
  • 26
  • 52
1

If all fails there is an option to resize UIImage's with SFSymbols. If you don't mind making the program a bit longer, I would recommend using,

extension UIImage {
    func resized(to newSize: CGSize) -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
        defer { UIGraphicsEndImageContext() }

        draw(in: CGRect(origin: .zero, size: newSize))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

which resizes the UIImage. So you can initialize your Image with a resized UIImage. The code will look something like...

Image(uiImage: UIImage(systemName: "plus.circle.fill")!.resized(to: CGSize(width: 40, height: 40)))

Visal Rajapakse
  • 1,718
  • 1
  • 9
  • 15