Cub*_*lax 34 uibutton uiview ios swiftui sf-symbols
我正在声明一个这样的按钮:
let menuButton = UIButton()
Run Code Online (Sandbox Code Playgroud)
之后,我尝试更改它的参数并在具有此功能的 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
}
Run Code Online (Sandbox Code Playgroud)
一切正常,我得到了正确的颜色、位置和大小。唯一的问题是它的大小,我不能用所有常用的方法来改变它menuButton.imageView?.contentMode = .scaleAspectFit。
可能是因为它是SF符号而不是普通图像。
有人的帮助将不胜感激。
谢谢
use*_*632 78
您可以使用 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)
Run Code Online (Sandbox Code Playgroud)
Spi*_*pau 20
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
Run Code Online (Sandbox Code Playgroud)
目标C:
UIImageSymbolConfiguration * configuration = [UIImageSymbolConfiguration configurationWithPointSize:30 weight:UIImageSymbolWeightBold scale:UIImageSymbolScaleLarge];
UIImage * image = [UIImage systemImageNamed:@"" withConfiguration:configuration];
[_btn setImage:image forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9981 次 |
| 最近记录: |