简体   繁体   中英

SF Symbol imageView does not conform to pointSize?

I have an empty UIImageView in my storyboard, and have the outlet in my ViewController.swift file. In viewDidLoad, I'm calling the below code:

let config = UIImage.SymbolConfiguration(pointSize: 50, weight: .light)
self.symbol.image = UIImage(systemName: "calendar")
self.symbol.preferredSymbolConfiguration = config

The symbol loads fine, and even the weight is correctly reflected.

However, the size of the symbol image is still tied to the size of the UIImageView in the storyboard even though I have no constraints set on the UIImageView. And from what I can tell the pointSize declared in SymbolConfiguration has no effect on the size of the symbol image.

From what I can tell it seems that there are two ways to get pointSize in SF Symbols to work: 1. Do all of it programmatically or 2. do all of it in Storyboard / Inspector.

Does anyone know what I'm doing wrong or does SF Symbols not support Storyboard + code configuration?

I figured it out, you have to call sizeToFit() after setting the configuration. I'm guessing Apple left it for the dev to call it so that they could prevent unnecessary recalculation of UI sizing thus potentially improving efficiency.

Anyway, the solution is: call sizeToFit() on the UIImageView after setting the configuration.

I solved the problem by changing the contentMode property of the UIImageView object to .center .

let imageView = UIImageView()

imageView.contentModel = .center // This is the key step.

imageView.preferredSymbolConfiguration = .init(pointSize: 10)
iconView.image = UIImage(systemName: "name")

Try this way instead:

let config = UIImage.SymbolConfiguration(pointSize: 50, weight: .light)
self.symbol.image = UIImage(systemName: "calendar", withConfiguration: config)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM