更改 UIButton 内的 SF 符号大小
Change a SF Symbol size inside a UIButton
我正在这样声明一个 Button :
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符号而不是普通图像。
非常感谢有人的帮助。
谢谢
您可以使用 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)
Objective C:
UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightBold scale:UIImageSymbolScaleLarge];
UIImage * image = [UIImage systemImageNamed:@"" withConfiguration:configuration];
[_btn setImage:image forState:UIControlStateNormal];
我正在这样声明一个 Button :
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符号而不是普通图像。
非常感谢有人的帮助。
谢谢
您可以使用 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)
Objective C:
UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightBold scale:UIImageSymbolScaleLarge];
UIImage * image = [UIImage systemImageNamed:@"" withConfiguration:configuration];
[_btn setImage:image forState:UIControlStateNormal];