【问题标题】:How do I put the image on the right side of the button and the text align left如何将图像放在按钮的右侧并且文本向左对齐
【发布时间】:2015-01-21 15:15:35
【问题描述】:

我有一个带有图片和文字的UIButton。如何让图片右对齐,文字左对齐?

【问题讨论】:

标签: ios uibutton xcode6 imageedgeinsets


【解决方案1】:

使用以下两种方法

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)

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

【讨论】:

    【解决方案2】:
    @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;
                }
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-22
      • 2017-01-25
      • 1970-01-01
      • 2021-05-02
      相关资源
      最近更新 更多