簡體   English   中英

更改 UIButton 內的 SF 符號大小

[英]Change a SF Symbol size inside a UIButton

我正在聲明一個這樣的按鈕:

let menuButton = UIButton()

之后,我嘗試更改它的參數並在 LBTATools(一個 pod)的幫助下使用此功能設置他在視圖上的位置:

fileprivate func setMenuButtonUI() {
        menuButton.setImage(UIImage(systemName: "line.horizontal.3.decrease.circle.fill"), for: .normal)
        view.addSubview(menuButton)
        menuButton.anchor(top: view.safeAreaLayoutGuide.topAnchor, leading: view.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 20, left: 60, bottom: 0, right: 0), size: .init(width: 40, height: 40))
        menuButton.setupShadow(opacity: 1, radius: 5, offset: .zero, color: .darkGray)
        menuButton.tintColor = .red
    }

一切運行良好,我得到了正確的顏色、位置甚至尺寸。 唯一的問題是它的大小,我不能用所有常用的方法來改變它,比如menuButton.imageView?.contentMode = .scaleAspectFit等。

可能是因為它是 SF Symbol 而不是普通圖像。

某人的幫助將不勝感激。

謝謝

您可以使用SymbolConfiguration來做到這一點,如下面的示例代碼所示:

let largeConfig = UIImage.SymbolConfiguration(pointSize: 140, weight: .bold, scale: .large)
       
let largeBoldDoc = UIImage(systemName: "doc.circle.fill", withConfiguration: largeConfig)

button.setImage(largeBoldDoc, for: .normal)

在此處輸入圖像描述

目標 C:

UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightBold scale:UIImageSymbolScaleLarge];
UIImage * image = [UIImage systemImageNamed:@"" withConfiguration:configuration];
[_btn setImage:image forState:UIControlStateNormal];

iOS 15 和 Swift 5 解決方案是:

var config = UIButton.Configuration.plain() // Change style with e.g. `.filled()`
config.title = "Title of the UIButton"
config.image = UIImage(systemName: "line.horizontal.3.decrease.circle.fill",
                                   withConfiguration: UIImage.SymbolConfiguration(scale: .small)) // Change the scale here
config.imagePadding = 4 // Padding between image and title
            
let menuButton = UIButton()
menuButton.configuration = config

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM