【问题标题】:UIButton titleLabel has paddingUIButton titleLabel 有填充
【发布时间】:2022-11-29 01:29:38
【问题描述】:

UIButton 标题标签有顶部和底部填充,我想删除填充。 设置 UIButton 内容模式无效。

这是我的代码

    lazy var button: UIButton = {
        let button = UIButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitleColor(ThemeColor.red, for: .normal)
        button.setTitle("Push", for: .normal)
        button.addTarget(self, action: #selector(buttonDidTap), for: .touchUpInside)
        button.backgroundColor = .blue
        button.contentHorizontalAlignment = .fill
        button.contentVerticalAlignment = .fill
        button.contentMode = .scaleAspectFill
        return button
    }()

看起来像

我怎样才能删除填充空间!

【问题讨论】:

标签: ios swift uikit


【解决方案1】:

正如 Matt 指出的那样,您可以通过调整按钮的 contentEdgeInsets 来解决此问题

但是,我注意到一件事,如果将 contentEdgeInsets 全部设置为 0:

button.contentEdgeInsets = UIEdgeInsets(top: 0,
                                        left: 0,
                                        bottom: 0,
                                        right: 0)

由于某种原因,您仍然会得到垂直填充。

我记得看到一个我现在找不到的答案,它建议设置一个非常小的边缘插入,这应该有效:

lazy var button: UIButton = {
    let button = UIButton(type: .custom)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.setTitleColor(.red, for: .normal)
    button.setTitle("Push", for: .normal)
    button.backgroundColor = .blue
    button.contentHorizontalAlignment = .fill
    button.contentVerticalAlignment = .fill
    button.contentMode = .scaleAspectFill
    button.contentEdgeInsets = UIEdgeInsets(top: .leastNormalMagnitude,
                                            left: .leastNormalMagnitude,
                                            bottom: .leastNormalMagnitude,
                                            right: .leastNormalMagnitude)
    return button
}()

【讨论】:

  • “contentEdgeInsets”在 iOS 15.0 中已弃用:使用 UIButtonConfiguration 时将忽略此属性
【解决方案2】:

您可以使用按钮的配置来更精确地控制其外观,如下所示:

lazy var myButton: UIButton = {
    let newButton = UIButton()

    newButton.translatesAutoresizingMaskIntoConstraints = false
    newButton.contentMode = .scaleAspectFill
    newButton.addTarget(self, action: #selector(buttonDidTap), for: .touchUpInside)

    // 'configurationUpdateHandler' property can be used to set appearance depending on its state
    newButton.configurationUpdateHandler = { button in 
        switch button.state { // here i'll just use default so it's the same over all states
        default:
            button.configuration?.title = "Push"
            button.configuration?.baseBackgroundColor = .blue

            // remove padding here
            button.configuration?.contentInsets.top = 0
            button.configuration?.contentInsets.bottom = 0
        }
    }
    
    return newButton
}()

当然你不需要使用updateHandler,你可以直接访问配置并在那里设置它

button.configuration?.contentInsets.top = 0
button.configuration?.contentInsets.bottom = 0

看看这是否解决了问题......

【讨论】:

    【解决方案3】:

    您可以使用UIButton.Configuration,然后将其contentInsets设置为.zero

    lazy var button: UIButton = {
        let button = UIButton(type: .custom)
        button.translatesAutoresizingMaskIntoConstraints = false
        var configuration = UIButton.Configuration.plain()
        configuration.background.backgroundColor = .blue
        configuration.background.cornerRadius = 0
        configuration.baseForegroundColor = .red
        configuration.title = "Push"
        configuration.contentInsets = .zero
        button.configuration = configuration
        return button
    }()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2019-07-11
      • 2020-05-21
      • 2013-09-10
      相关资源
      最近更新 更多