How do I put the image on the right side of the text in a UIButton?
Despite some of the suggested answers being very creative and extremely clever, the simplest solution is as follows:
button.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
As simple as that. As a bonus, the image will be at the left side in right-to-left locales.
EDIT: as the question has been asked a few times, this is iOS 9 +.
Align image at the right corner of UIButton irrespective of text length
Use override
method of UIButton
and manage your title and image frame.
Here is UIButton
subclass.
class ButtonIconRight: UIButton {
override func imageRect(forContentRect contentRect:CGRect) -> CGRect {
var imageFrame = super.imageRect(forContentRect: contentRect)
imageFrame.origin.x = self.bounds.width - imageFrame.width
return imageFrame
}
override func titleRect(forContentRect contentRect:CGRect) -> CGRect {
var titleFrame = super.titleRect(forContentRect: contentRect)
if (self.currentImage != nil) {
titleFrame.origin.x = 0
}
return titleFrame
}
}
How to add Image on UIButton on right side and contentMode don't working with image?
Try this code and modify EdgeInset as you want, and assign your outlet as DropdownButton
instead of IUButton
class DropdownButton: UIButton {
override func layoutSubviews() {
super.layoutSubviews()
self.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
if self.imageView?.image != nil {
// Move icon to right side
self.imageEdgeInsets = UIEdgeInsets(
top: 0,
left: self.bounds.size.width - self.imageView!.image!.size.width - 10,
bottom: 0,
right: 0)
// Move title to left side
self.titleEdgeInsets = UIEdgeInsetsMake(5, -self.imageView!.frame.size.width + 10, 5, 5)
}
}
}
Aligning text and image on UIButton with imageEdgeInsets and titleEdgeInsets
I agree the documentation on imageEdgeInsets
and titleEdgeInsets
should be better, but I figured out how to get the correct positioning without resorting to trial and error.
The general idea is here at this question, but that was if you wanted both text and image centered. We don't want the image and text to be centered individually, we want the image and the text to be centered together as a single entity. This is in fact what UIButton already does so we simply need to adjust the spacing.
CGFloat spacing = 10; // the amount of spacing to appear between image and title
tabBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
tabBtn.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
I also turned this into a category for UIButton so it will be easy to use:
UIButton+Position.h
@interface UIButton(ImageTitleCentering)
-(void) centerButtonAndImageWithSpacing:(CGFloat)spacing;
@end
UIButton+Position.m
@implementation UIButton(ImageTitleCentering)
-(void) centerButtonAndImageWithSpacing:(CGFloat)spacing {
self.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, spacing);
self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
}
@end
So now all I have to do is:
[button centerButtonAndImageWithSpacing:10];
And I get what I need every time. No more messing with the edge insets manually.
EDIT: Swapping Image and Text
In response to @Javal in comments
Using this same mechanism, we can swap the image and the text. To accomplish the swap, simply use a negative spacing but also include the width of the text and the image. This will require frames to be known and layout performed already.
[self.view layoutIfNeeded];
CGFloat flippedSpacing = -(desiredSpacing + button.currentImage.size.width + button.titleLabel.frame.size.width);
[button centerButtonAndImageWithSpacing:flippedSpacing];
Of course you will probably want to make a nice method for this, potentially adding a second category method, this is left as an exercise to the reader.
How do I put the image on the right side of the text in a UIButton?
Despite some of the suggested answers being very creative and extremely clever, the simplest solution is as follows:
button.semanticContentAttribute = UIApplication.shared
.userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
As simple as that. As a bonus, the image will be at the left side in right-to-left locales.
EDIT: as the question has been asked a few times, this is iOS 9 +.
Related Topics
iOS 6 Facebook Posting Procedure Ends Up with "Remote_App_Id Does Not Match Stored Id"
How to Pass Data from Modal View Controller Back When Dismissed
"Reading from Public Effective User Settings" in iOS 10
How to Detect Swipe Gesture in iOS
Custom Alert (Uialertview) with Swift
How to Access File Included in App Bundle in Swift
Uiimage Imagenamed Returns Nil
Set Default iOS Local Notification Style for Application
How to Make a Vertical Text Uilabel and Uitextview for iOS in Swift
Using Uiimagepickercontroller in Landscape Orientation
Application Crashed While Importing Songs from Ipod Library in iPhone for iOS 5.0
Uitextfield Securetextentry Bullets with a Custom Font
Autolayout + Rtl + Uilabel Text Alignment
How to Fill a Uibezierpath with a Gradient
How to Get iOS Device MAC Address Programmatically
Table View Images Never Being Released
Xcode Canvas for Swiftui Previews Does Not Show Up
iOS - Mkmapview Place Annotation by Using Address Instead of Lat/Long