1

我有一个UIButton图像和文字。如何将图像对齐到右侧,将文本对齐到左侧?

4

2 回答 2

0

使用以下两种方法

button.imageEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)
button.titleEdgeInsets = UIEdgeInsets(top: 0.0, left: 0, bottom: 0.0, right: 0.0)

传递左右的值并调整它。根据您的要求设置偏移量。

于 2016-12-02T10:58:42.410 回答
0
@IBDesignable class RightImageButton: UIButton {

    @IBInspectable var alignImageToRight: Bool = true

    override func layoutSubviews() {
        super.layoutSubviews()
        if alignImageToRight {
            if let imageView = self.imageView, let titleLabel = self.titleLabel {
                let imageWidth = imageView.frame.size.width
                var imageFrame: CGRect = imageView.frame;
                var labelFrame: CGRect = titleLabel.frame;
                labelFrame.origin.x = self.titleEdgeInsets.left + self.contentEdgeInsets.left
                imageFrame.origin.x = frame.width - self.imageEdgeInsets.right - self.contentEdgeInsets.right - imageWidth
                imageView.frame = imageFrame;
                titleLabel.frame = labelFrame;
            }
        }
    }

}
于 2016-12-02T10:53:22.187 回答