1

我定义了以下 UIButton:

internal lazy var volumeUnitSelector: DropDownButton = {

    let button = DropDownButton()
    button.setTitle("impg", for: .normal)
    button.setTitleColor(UIColor.darkGreyWithDarkMode.withAlphaComponent(0.7), for: .normal)
    
    button.titleLabel?.font = .systemFont(ofSize: 14)
    button.titleLabel?.textAlignment = .right
    
    if #available(iOS 13.0, *) {
        let config = UIImage.SymbolConfiguration(pointSize: 8, weight: .black)
        let symbol = UIImage(systemName: "arrowtriangle.down.fill", withConfiguration: config)?.withTintColor(UIColor.darkGreyWithDarkMode, renderingMode: .alwaysOriginal)
        button.setImage(symbol, for: .normal)
    }
    return button
}()

这是 UIButton 的子类的 DropDownButton 类:

class DropDownButton: UIButton {

    override func layoutSubviews() {
        self.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0.0, bottom: 0.0, right: 9)    

        super.layoutSubviews()
        if imageView != nil {

            imageView?.translatesAutoresizingMaskIntoConstraints = false
            imageView?.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
            imageView?.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
            self.widthAnchor.constraint(equalToConstant: 43).isActive = true
        }
    }
}

这是挑战 - 我正在尝试将此按钮设置为具有文本“impg”,然后是一个箭头以指示文本右侧的下拉菜单。如您所见,我已将箭头添加为 SFSymbol 并将其固定在按钮的右侧。然后,我需要将文本基本上固定到箭头图像的leadingAnchor,并留有很小的边距(0.5 / 1 点)。

因此,我将右侧的 titleEdgeInset 设置为 9,以便为图像腾出空间。现在这是一种工作,但我不明白我的标题正在被剪裁,而应该有足够的空间。

这是它的当前效果:

在此处输入图像描述

这是 xCode 布局视图:

在此处输入图像描述

我不明白为什么文本左侧没有足够的空间来展开。我没有设置其他约束。

4

0 回答 0