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 button.
This has no effect:
button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 50);
How can one right-align the image for a button? Can it be done in the Storyboard?
Storyboard:
Attributes Tab > Ensure your Content Edge tab is selected for 'image':
Then you alter the 'Left' property not right, which is what your doing programatically. So in other words, your padding it n from the left
UIEdgeInsetsMake = TOP | LEFT | BOTTOM | RIGHT
Programmatically :
button.imageEdgeInsets = UIEdgeInsetsMake(0, 100, 0, 0);
Note: you might have to also alter your titles inset, depending on where it is in reference to your image
I found a tricky way
Update the constrains for the UIImageView
of the Button
try this
button.imageView?.trailingAnchor.constraint(equalTo: button.trailingAnchor, constant: -8.0).isActive = true
button.imageView?.centerYAnchor.constraint(equalTo: button.centerYAnchor, constant: 0.0).isActive = true
but don't forget to add this to make the constrains effective
button.translatesAutoresizingMaskIntoConstraints = false
button.imageView?.translatesAutoresizingMaskIntoConstraints = false
Clean way to do this in Swift
button.semanticContentAttribute = .forceRightToLeft
Hope this helps
There are several different options to do it.
Use semanticContentAttribute
button.semanticContentAttribute = .forceRightToLeft
You can set semantic attribute from interface (storyboard) layout also.
or
Use UIView containing UIButton and UIImage, as shown in this snapshot.
- Set Image right side in UIView and disable user interaction, so user action/tap will be passed to button.
- Add button with size same as UIView, in UIView, with transparent background color and covering Image.
- Set content offset for button as shown in this snapshot.
This will make easy to handle button action, properties and customise image position & size according to your requirement. Try this.
make it as default semantic i.e unspecified or force left to right
and in button.imageEdgeInsets set it as
UIEdgeInsets(top: 0, left: self.view.frame.size.width - (the image size + the alignment space ) , bottom: 0, right: 0)
this will make ensure that no matter what the view size is ,it will always align the image from right side of the button
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
}
}
Try below code:
btn.contentHorizontalAlignment = .right
Just like this:
btn.imageView?.translatesAutoresizingMaskIntoConstraints = false
btn.imageView?.centerYAnchor.constraint(equalTo: btn.centerYAnchor, constant: 0.0).isActive = true
btn.imageView?.trailingAnchor.constraint(equalTo: btn.trailingAnchor, constant: 0.0).isActive = true
btn.imageView?.contentMode = .right
It's working for me:
self.acceptButton.setImage(UIImage(named: image), for: UIControl.State.normal)
self.acceptButton.semanticContentAttribute = .forceRightToLeft
self.acceptButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 9, bottom: 0, right: 0)
This worked for me by setting:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
Then for the inner image I set the right inset to 0.
button.imageEdgeInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 0);
来源:https://stackoverflow.com/questions/32365521/align-button-image-to-right-edge-of-uibutton