1

I have a question about the sizing for UIImage in UIbutton.

I'm trying to make a UIButton with SF symbols (xmark.circle) to add a dismissal or close button to my app and make it bigger. the icon is like this ... enter image description here

To resize the SF symbols, I saw one developer uses withConfiguration and make the UIimage larger, and I checked it works in my project.

lazy var dismissButton: UIButton = {
    let button = UIButton()
    let large = UIImage.SymbolConfiguration(pointSize: 32, weight: .regular, scale: .default)
    let largeImg = UIImage(systemName: "xmark.circle", withConfiguration: large)
    button.isUserInteractionEnabled = true
    button.setImage(largeImg, for: .normal)
    button.tintColor = .lightGray
    return button
}()

However, I already have a struct for UIimage and since I did not add withConfiguration, I tried to add the withConfiguration when I create a button in code.

I mean, I have the Images struct

struct Images {
    static let minus = UIImage(systemName: "minus")
    static let trash = UIImage(systemName: "trash")
    static let pencil = UIImage(systemName: "pencil")
    static let xmarkCircle = UIImage(systemName: "xmark.circle")
    // with more images...
}

and I want to call the Images struct when I create a button

lazy var dismissButton: UIButton = {
    let button = UIButton()
    button.isUserInteractionEnabled = true
    button.setImage(Images.xmarkCircle, for: .normal) // -> like in here
    button.tintColor = .lightGray
    return button
}()

But in this case, since I just call the icon, Images.xmarkCircle, in setImage, I cannot assign withConfiguration to that icon. Probably, I can change Images.xmarkCircle to UIImage with configuration or something, but I don't want to make one of the icons in images struct has withConfiguration value.

So, is it possible to add withConfiguration when I make a button in lazy var dismissButton, or is there another way to resize the SFsymbol in code?

Yuuu
  • 715
  • 1
  • 9
  • 32
  • 2
    I guess you've found [this post](https://stackoverflow.com/questions/60910712/resizing-uimage-when-using-sf-symbols-uiimagesystemname), and saw Rob's answer? Did you see the other answer? It contains the answer your need - `applyingSymbolConfiguration`. – Sweeper Aug 13 '21 at 04:09
  • @Sweeper Thank you for your reply. Yeah, I saw people using SymbolConfiguration and in the first code block in my post, I wrote like that, and I know it works. But, I have struct Images in my project already and I put every UIimage in that struct. So I don't want to call let largeImg = UIImage(systemName: "xmark.circle", withConfiguration: large) in the lazy var dismissButton. – Yuuu Aug 13 '21 at 04:18
  • @Sweeper So you mean I must declare UIimage.SymbolConfiguration inside of lazy var dismissButton? and I cannot use UIImage(systemName: "xmark.circle") in Images struct? – Yuuu Aug 13 '21 at 04:23
  • One option is to replace the constants with methods to which you can optionally supply a point size, e.g. https://gist.github.com/robertmryan/a3acce637929bb75efa40c934681363b. – Rob Aug 13 '21 at 16:43

0 Answers0