ios UIButton标题标签有填充

xuo3flqw  于 2022-12-01  发布在  iOS
关注(0)|答案(3)|浏览(175)

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
    }()

看起来像x1c 0d1x
我怎么能删除填充空间!

e3bfsja2

e3bfsja21#

正如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
}()

0vvn1miw

0vvn1miw2#

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

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

看看这能不能解决问题...

km0tfn4u

km0tfn4u3#

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

相关问题