英文:
does not display a picture from the sf symbol directory
问题
我是一个初学者,在屏幕中央显示了一张图片(这是一个原型,用于检查是否一切正常),但是图片没有显示出来,我完全复制了名称,但没有效果。使用UIImage(systemName: "")也不起作用。但如果我使用来自Assets.xcassets的图片,一切都显示。
我的代码:
```swift
override func viewDidLoad() {
super.viewDidLoad()
let imageView = setupImageView(in: view)
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
func setupImageView(in view: UIView) -> UIImageView {
let imageView = UIImageView()
imageView.image = UIImage(systemName: "pencil.and.outline")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageView)
return imageView
}
一个空屏幕与sfsymbol的调用,一个有狗的屏幕与资源一起显示。
我向具有聊天经验的程序员请教过,但没有人遇到过这样的问题。
<details>
<summary>英文:</summary>
I am a beginner and I have a picture displayed in the center of the screen (this is a prototype to check if everything works), but the picture is not displayed, I copied the name completely, but it does not work. does not work with UIImage(systemName: ""). but if i use picture from Assets.xcassets but everything shows.
my code:
override func viewDidLoad() {
super.viewDidLoad()
let imageView = setupImageView(in: view)
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
}
func setupImageView(in view: UIView) -> UIImageView {
let imageView = UIImageView()
imageView.image = UIImage(systemName: "pencil.and.outline")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageView)
return imageView
}
an empty screen with a call is a call from sfsymbol, a screen with a dog is a screen with assests
[![](https://i.stack.imgur.com/KwALG.jpg)](https://i.stack.imgur.com/KwALG.jpg)
[![enter image description here][1]][1]
I asked programmers with experience in chat, but no one faced such a problem
[1]: https://i.stack.imgur.com/JrjOA.png
</details>
# 答案1
**得分**: 0
根据您的截图和测试了您的代码,似乎 SF Symbol 确实被显示出来了。
只是颜色的问题。因为您在背景中使用了 `.link` 颜色,这恰好是 SF Symbols 的默认颜色,它会被正确添加为子视图,但您只是看不到它。
可能的解决方案:
1. 更改 SF Symbol 的颜色:
```swift
imageView.image = UIImage(systemName: "pencil.and.outline")?.withTintColor(.red, renderingMode: .alwaysOriginal)
- 更改视图的背景颜色:
view.backgroundColor = .white
提示:如果您通过“调试视图层次结构”检查您的 UI,这是一个非常有用的工具,您应该能够看到它并轻松解决这类问题。
英文:
Judging by you screenshots and having tested your code, it seems that the SF Symbol is indeed displayed.
It's just a matter of colors. Since you're using .link
color for the background, which is the same color of SF Symbols by default, it is correctly added as a subview but you just can't see it.
Possible solutions:
-
change SF Symbol color:
imageView.image = UIImage(systemName: "pencil.and.outline")?.withTintColor(.red, renderingMode: .alwaysOriginal)
-
change view's background color:
view.backgroundColor = .white
Tip: if you inspect your UI through Debug View Hierarchy, a very useful tool, you should be able to see it and solve these kind of issues easily.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论