简体   繁体   English

SF Symbol imageView 不符合pointSize?

[英]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.我的 storyboard 中有一个空的 UIImageView,并在我的 ViewController.swift 文件中有插座。 In viewDidLoad, I'm calling the below code:在 viewDidLoad 中,我调用了以下代码:

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.但是,符号图像的大小仍然与 storyboard 中的 UIImageView 的大小相关,即使我没有对 UIImageView 设置任何限制。 And from what I can tell the pointSize declared in SymbolConfiguration has no effect on the size of the symbol image.据我所知,在 SymbolConfiguration 中声明的 pointSize 对符号图像的大小没有影响。

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.据我所知,似乎有两种方法可以让 SF Symbols 中的 pointSize 起作用:1. 以编程方式完成所有操作或 2. 在 Storyboard / Inspector 中完成所有操作。

Does anyone know what I'm doing wrong or does SF Symbols not support Storyboard + code configuration?有谁知道我做错了什么或者 SF Symbols 不支持 Storyboard + 代码配置?

I figured it out, you have to call sizeToFit() after setting the configuration.我想通了,你必须在设置配置后调用sizeToFit() 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.我猜苹果把它留给了开发人员来调用它,这样他们就可以防止不必要地重新计算 UI 大小,从而有可能提高效率。

Anyway, the solution is: call sizeToFit() on the UIImageView after setting the configuration.无论如何,解决方案是:设置配置后在 UIImageView 上调用sizeToFit()

I solved the problem by changing the contentMode property of the UIImageView object to .center .我通过将UIImageView object 的contentMode属性更改为.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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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