How to make custom UIBarButtonItem with image and label?

I would like to make a custom UIBarButtonItem that contains both image and text, something like this:

How to make custom UIBarButtonItem with image and label?

I tried subclassing UIBarButtonItem and overriding this method:

- (UIView *)customView
    if (!self.storedView) {

        UIView *temp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:self.image];
        tempImageView.frame = CGRectMake(0, 0, self.image.size.width, self.image.size.height);
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(44, 0, 100, 44)];
        tempLabel.text = @"text";
        [temp addSubview:tempImageView];
        [temp addSubview:tempLabel];
        self.storedView = temp;
    return self.storedView;

And I use it like this:

UIBarButtonItem *left = [[LeftItem alloc] initWithTitle:@"Settings" style:UIBarButtonItemStylePlain target:self action:@selector(settingsPressed)];
left.title = @"Settings";
left.image = [UIImage imageNamed:@"settings.png"];
self.navigationItem.leftBarButtonItem = left;

But using this I only get the image, but not label. What am I doing wrong?

How can I make UIBarButtonItem image transparent?

I’d like to use the attached png as an image of an UIBarButtonItem. I’d like to set its black background as transparent, but I can’t find any way to set it as mask color in XCode. How can I make it tr

How to make UIBarButtonItem same click effect as UIButton?

How to make UIBarButtonItem with properties(Style: Bordered and Identifier: Custom ) click effect (like the UIButton selected state) ??

Creating a custom uibarbuttonitem, button image wrong

I am trying to create a custom UiBarButtonItem. When I build the project on IOS simulator, the custom buttons proportions are all wrong. I have tried different sizes of image and they all don’t work.

How to make a UIBarButtonItem like this

How can I make a UIBarButtonItem like this: I can’t find it in the SystemItem values. Thanks

UIBarButtonItem with custom image and no border

I want to create a UIBarButtonItem with a custom image, but I don’t want the border that iPhone adds, as my Image has a special border. It’s the same as the back button but a forward button. This App

How to add a UIBarButtonItem of custom width to a UINavigationItem programmatically without using an image

I’m using iOS 6.1 and want to add a UIBarButtonItem of custom width with title Info to the UINavigationItem programmatically when the view loads. I don’t want to use an image to achieve this. I read

How to add an image to UIBarButtonItem?

I created a custom button and applied it to the UIbarbuttonitem. There is no error but nothing is shown on the navigation bar:( This is my code- //create a custom button UIImage *image = [UIImage ima

iOS, how to make a custom uibarbuttonitem with no-border, no-span with each other? like this picture?

iOS, how to make a custom uibarbuttonitem with no-border, no-span with each other? like this picture? I do not know how to name this style. all these buttons are on the UIToolBar component. thank yo

How to set custom view for UIBarButtonItem?

I am trying to put a label in a UIBarButtonItem. Here is my viewDidLoad method: characterCountLabel = [[UILabel alloc] init]; characterCountLabel.text = @HELLO; charCountButton = [[UIBarButtonItem a

Custom uibarbuttonitem image to blend with background

I am making a custom UI for Navigation bar and I have a question exactly as asked in this post Custom background/transparent background on UIBarButtonItem? In StoryBoard I tried every sort of combina


 UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [button addTarget:target action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 53, 31)];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
    [label setFont:[UIFont fontWithName:@"Arial-BoldMT" size:13]];
    [label setText:title];
    label.textAlignment = UITextAlignmentCenter;
    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor clearColor]];
    [button addSubview:label];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButton;
UIImage *image = [UIImage imageNamed:@"icon.png"];
UIImage *backgroundSelected = [UIImage imageNamed:@"icon_selected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(ButtonTapped:event:)forControlEvents:UIControlEventTouchUpInside]; //adding action
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setBackgroundImage:backgroundSelected forState:UIControlStateSelected];
button.frame = CGRectMake(0 ,0,35,35);

then make that button as your custom barbutton

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButton;

You can add a custom view to the UIBarButtonItem.

In iOS7, there is a new buttonType called UIButtonTypeSystem for UIButton which serve your purpose. Try this,

    UIView* leftButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 110, 50)];

    UIButton* leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.backgroundColor = [UIColor clearColor];
    leftButton.frame = leftButtonView.frame;
    [leftButton setImage:[UIImage imageNamed:<YourImageName>] forState:UIControlStateNormal];
    [leftButton setTitle:@"YourTitle" forState:UIControlStateNormal];
    leftButton.tintColor = [UIColor redColor]; //Your desired color.
    leftButton.autoresizesSubviews = YES;
    leftButton.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    [leftButton addTarget:self action:@selector(<YourTargetMethod>) forControlEvents:UIControlEventTouchUpInside];
    [leftButtonView addSubview:leftButton];

    UIBarButtonItem* leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButtonView];
    self.navigationItem.leftBarButtonItem = leftBarButton;

The problem with the accepted answer is that the UIBarButtonItem text does not change when highlighted. The code below will display text plus an image. The UIEdgeInsetsMake moves the text to the left and the image to the right.

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(myButtonEvent:) 
[button setFrame:CGRectMake(0, 0, 53, 32)];

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] 
[button setTitleColor:[UIColor lightGrayColor] 
button.titleEdgeInsets = UIEdgeInsetsMake(-2, -20, 2, 20);
button.titleLabel.font = font;
button.titleLabel.textAlignment = NSTextAlignmentLeft;

UIImage *image = [UIImage imageNamed:@"imageName"];
[button setImage:image 
button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32);
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];

If your BarButtonItem is in Storyboard, you can drag another Button into BarButtonItem, so there will be a Button inside the BarButtonItem, you can add both image and label to the button.

I posted answer that handles tint colour and image simmilar to normal uibarbuttonitem here…

Another approach with NSAttributedString:

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Button Text"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"buttonImage"];
textAttachment.bounds = CGRectMake(0, -3, textAttachment.image.size.width, textAttachment.image.size.height); //the origin y value depends on the size of the image to get a perfect fit
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(0, 0) withAttributedString:attrStringWithImage]; // Adding the image at the beginning
[customButton setAttributedTitle:attributedString forState:UIControlStateNormal];
[customButton sizeToFit];
[customButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithCustomView:customButton];

Using UIButton as custom view inside your UIBarButtonItem. You can create UIButton however you want, including with an image and text, using -setImage:forState: and -setTitle:forState:

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
[button setTitle:@"Settings" forState:UIControlStateNormal];
[button addTarget:target action:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[button sizeToFit];
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = barButtonItem;

this can be done using interface builder just by dragging a UIButton to the left bar button slot on a navigation bar.

a SWIFT version

    let button =  UIButton(type: .Custom)
    button.setImage(UIImage(named: "icon_right"), forState: .Normal)
        button.addTarget(self, action: "buttonAction", forControlEvents: .TouchUpInside)
    button.frame = CGRectMake(0, 0, 53, 31)
    button.imageEdgeInsets = UIEdgeInsetsMake(-1, 32, 1, -32)//move image to the right
    let label = UILabel(frame: CGRectMake(3, 5, 50, 20))
    label.font = UIFont(name: "Arial-BoldMT", size: 16)
    label.text = "title"
    label.textAlignment = .Center
    label.textColor = UIColor.whiteColor()
    label.backgroundColor =   UIColor.clearColor()
    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.rightBarButtonItem = barButton

Awesome solution! I was looking to add a UISlider into a UIToolbar, and I did it just this way! Don’t forget to add a target action for your custom buttons via:

  customButton.targetForAction(#selector(addTapped), withSender: self)

This way you can control your buttons šŸ™‚