【发布时间】:2021-02-11 13:38:22
【问题描述】:
请您帮我解释一下为什么按钮标题的填充不起作用?
我在下面的代码中更新了按钮的标题 n 次。按钮位于水平滚动视图内。在滚动视图里面我有一个堆栈视图。在滚动之外,我更新了标题,并且填充超出了我的控制范围。
问题如下:如果我为按钮提供了一个大标题,然后我更改为一个短标题,则填充不会按照我在下面的代码中确定的那样更新。
private func setupActionButtons(index: Int = 0, title: String = "") {
allButtons[index].layer.cornerRadius = allButtons[0].bounds.height/2
allButtons[index].layer.borderWidth = 1
allButtons[index].layer.borderColor = UIColor.borderShortcutButton.cgColor
allButtons[index].setTitleColor(.psGreyValueLabelFilled, for: .normal)
if title != "" {
allButtons[index].setTitle(title, for: .normal)
}
allButtons[index].titleEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
allButtons[index].contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 15)
allButtons[index].imageEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
allButtons[index].imageView?.contentMode = .scaleAspectFit
allButtons[index].setImage(uiImage, for: .normal)
allButtons[index].semanticContentAttribute = UIApplication.shared.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
allButtons[index].titleLabel?.font = UIFont.systemFont(ofSize: buttonFontSize, weight: UIFont.Weight.semibold)
allButtons[index].tag = index
allButtons[index].addTarget(self, action: #selector(self.handleSelectOption(sender:)), for: .touchUpInside)
}
解释得更好:当我在 stackview 中为两个按钮添加宽度相似的标题,然后我将其中一个按钮的标题更改为更小的宽度时,另一个按钮的宽度更大,填充更大,并且其他我更改的排列正确。
【问题讨论】:
-
你试过 button.titleLabel 吗?.adjustsFontSizeToFitWidth = true ?
-
快速测试,我没有发现任何问题。您说“按钮位于水平滚动视图视图中。滚动视图内部我有一个堆栈视图。”您的意思是按钮是堆栈视图的排列子视图吗?如果是这样,您配置/约束stackView的方式有问题吗?您需要提供更多信息。
-
@StefanOvomate 你的建议没有解决
-
@DonMag 按钮排列在 stackview 内。我发现stackview配置没有问题。配置听起来正确。
-
@DonMag 我在答案末尾添加了更多描述
标签: swift uiscrollview uibutton padding uistackview