How to add system icons for a UIButton programmatically?

前端 未结 4 1274
野趣味
野趣味 2021-02-19 10:54

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

4条回答
  •  忘了有多久
    2021-02-19 11:45

    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 InterfaceBuilder, select UIButton and in Attributes Inspector select Image which will give you list of all available icons.

提交回复
热议问题