Align button image to right edge of UIButton

前端 未结 14 2186
野趣味
野趣味 2020-12-07 23:05

There are plenty of threads about aligning a button image according to the title text, but I can\'t find anything about just aligning the image to the right side of the butt

14条回答
  •  囚心锁ツ
    2020-12-07 23:41

    Simple way

    Using extension to set image on the right side with custom offset

       extension UIButton {
        func addRightImage(image: UIImage, offset: CGFloat) {
            self.setImage(image, for: .normal)
            self.imageView?.translatesAutoresizingMaskIntoConstraints = false
            self.imageView?.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0.0).isActive = true
            self.imageView?.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -offset).isActive = true
        }
    }
    

提交回复
热议问题