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?
Asked
Active
Viewed 4.2k times
25

Community
- 1
- 1

DeyaEldeen
- 10,847
- 10
- 42
- 75
-
1You can find those icons online! Like here: https://icons8.com – Sweeper Jun 12 '16 at 09:07
5 Answers
48
For Swift 5 the syntax is:
button.setImage(UIImage(systemName: "search"), for: .normal)
You can also set the weight of the icon by adding SymbolConfiguration
:
let boldConfig = UIImage.SymbolConfiguration(weight: .bold)
let boldSearch = UIImage(systemName: "search", withConfiguration: boldConfig)
button.setImage(boldSearch, for: .normal)
See: Apple documentation for available names (API column) or go to Interface Builder, select UIButton and in the Attributes Inspector select Image which will give you list of all available icons.

shim
- 9,289
- 12
- 69
- 108

Marek Loose
- 1,115
- 13
- 17
-
1You need to set @available attributes to check for iOS versions. – Ashutosh Shukla Dec 03 '19 at 11:30
-
-
Why does this not work for me? My button just ends up empty. I have a conditional where the attempt to execute the above code will only happen if the iOS version is 13+ – Brian Reinhold Aug 05 '20 at 20:43
-
3
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)

Manikandan
- 1,195
- 8
- 26
3
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:
github: link

DeyaEldeen
- 10,847
- 10
- 42
- 75

Alexander
- 91
- 4
-2
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()
}

Fattie
- 27,874
- 70
- 431
- 719

Hritik Singh
- 101
- 7