'How to add system icons for a UIButton programmatically?

It's easy to add a custom image or a background for a UIButton , but there seems to be no programmatic way to set one of the following default iOS icons for a UIButton , I know it can be applied to navigation bar buttons, I don't need that, I want to apply it to a simple UIButton, any hints?

enter image description here



Solution 1:[1]

import UIKit

extension UIImage {

    public convenience init?(_ systemItem: UIBarButtonItem.SystemItem) {

        guard let sysImage = UIImage.imageFrom(systemItem: systemItem)?.cgImage else {
            return nil
        }

        self.init(cgImage: sysImage)
    }

    private class func imageFrom(systemItem: UIBarButtonItem.SystemItem) -> UIImage? {

        let sysBarButtonItem = UIBarButtonItem(barButtonSystemItem: systemItem, target: nil, action: nil)

        //MARK:- Adding barButton into tool bar and rendering it.
        let toolBar = UIToolbar()
        toolBar.setItems([sysBarButtonItem], animated: false)
        toolBar.snapshotView(afterScreenUpdates: true)

        if  let buttonView = sysBarButtonItem.value(forKey: "view") as? UIView{
            for subView in buttonView.subviews {
                if subView is UIButton {
                    let button = subView as! UIButton
                    let image = button.imageView!.image!
                    return image
                }
            }
        }
        return nil
    }
}

This is an example of how do we use it:

 let button = UIButton() ;
 let systemImage = UIImage(systemItem: .trash) ;
 button.setImage(systemImage, for: .normal)

Solution 2:[2]

I found 2521 system icons, as numerated values.

@available(iOS 13.0, *)
@objc public extension UIImage{

    var assetName: String? {
        guard let imageAsset = imageAsset else { return nil }
        return imageAsset.value(forKey:"assetName") as? String
    }

    static var square_and_arrow_up: UIImage? {
        return UIImage(systemName: "square.and.arrow.up")
    }
    static var square_and_arrow_up_fill: UIImage? {
        return UIImage(systemName: "square.and.arrow.up.fill")
    }
    static var square_and_arrow_down: UIImage? {
        return UIImage(systemName: "square.and.arrow.down")
    }
    ...

screenshot: screenshot github: link

Solution 3:[3]

Now you can just do

button.setImage(UIImage(.search), for: .normal)

Solution 4:[4]

    var logoImage :[UIImage?] = []
        
     // make an array of UIImage 
//load image in asset and append in array
        @IBOutlet weak var table1: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            logoImage.append(UIImage(named: "image0"))
            logoImage.append(UIImage(named: "image1"))
            logoImage.append(UIImage(named: "image2"))
            logoImage.append(UIImage(named: "image3"))
            logoImage.append(UIImage(named: "image4"))
    
    
    
    
     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            if let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as? CustomCell{
               cell.iconImage.image = self.logoImage[indexPath.row]
                return cell
            }
            return UITableViewCell()
        }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 DeyaEldeen
Solution 3 HoereeBauke
Solution 4 Hritik Singh