Search code examples
swiftuikitswift5sf-symbols

does not display a picture from the sf symbol directory


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

enter image description here

I asked programmers with experience in chat, but no one faced such a problem


Solution

  • 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:

    1. change SF Symbol color:

       imageView.image =  UIImage(systemName: "pencil.and.outline")?.withTintColor(.red, renderingMode: .alwaysOriginal)
      
    2. 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.

    enter image description here