【问题标题】:Change a SF Symbol size inside a UIButton更改 UIButton 内的 SF 符号大小
【发布时间】:2020-06-23 17:35:53
【问题描述】:

我正在声明一个这样的按钮:

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而不是普通图像。

非常感谢某人的帮助。

谢谢

【问题讨论】:

    标签: ios uiview swiftui uibutton sf-symbols


    【解决方案1】:

    您可以使用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)
    

    【讨论】:

    • 这在 UIButton 中使用 SF 符号时非常好。虽然使用 imageEdgeInsets 更改 UIButton 中的图像大小很棘手。
    • 我可以看到它没有居中。
    【解决方案2】:

    目标 C:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2016-08-24
      相关资源
      最近更新 更多