2014-11-23 90 views

回答

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; 
      } 
     } 
    } 

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

通行证值左右,调整。 按照您的要求设置偏移量。

相关问题