[英]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 而不是普通圖像。
某人的幫助將不勝感激。
謝謝
目標 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.