简体   繁体   English

如何以编程方式为 UIButton 添加系统图标?

[英]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?可以很容易地添加自定义图片或为背景UIButton ,但似乎没有编程的方式来为一个下列默认的iOS图标设置一个UIButton ,我知道它可以应用到导航栏按钮,我不知道需要那个,我想将它应用到一个简单的UIButton ,有什么提示吗?

在此处输入图片说明

For Swift 5 the syntax is:对于 Swift 5,语法是:

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

You can also set the weight of the icon by adding SymbolConfiguration :您还可以通过添加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.请参阅:有关可用名称的Apple 文档(API 列)或转到 Interface Builder,选择 UIButton 并在 Attributes Inspector 中选择 Image,它将为您提供所有可用图标的列表。

界面生成器图标

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)

I found 2521 system icons, as numerated values.我找到了 2521 个系统图标,作为数值。

@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 github: 链接

现在你可以做

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

来自 Apple 的文档: init(type:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM