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 +.
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.
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
}
}
UIButton title and image alignment query
Change the imageEdgeInsets
property on the UIButton
and then just use setImage:forState:
Related Topics
Multiline Uilabel with Adjustsfontsizetofitwidth
Space Between Sections in Uitableview
Add Subview from a Xib or Another Scene with Storyboard
App Crashes on Enabling Camera Access from Settings iOS 8
Adding Frameworks to Project in Xcode 5 and Having *Relative* Paths Added
Are Cookies in Uiwebview Accepted
How to Use Swift Repl with iOS Sdk
Can an iOS App Switch to Safari Without Opening a Page
Location Update Even When App Is Killed/Terminated
iOS Static Framework with Resources Inside
How to Keep an Iphone/iPad Web App in Full Screen Mode
Editing Screenshots in Itunes Connect After iOS App Was Approved
How to Check If a Uialertcontroller Is Already Presenting
Flutter iOS Build Failed an Error of Pod Files: Podfile Is Out of Date
iOS Provisioning and Certifcates - Will Revoke/Renew Effect App Store Apps
iOS Glsl. How to Create an Image Histogram Using a Glsl Shader
Run a Swift 2.0 App Forever in Background to Update Location to Server