How can I have a UIBarButtonItem with both image and text?

uibutton with image and text storyboard
uibutton image and text alignment
uibarbuttonitem image and text swift 4
uilabel with image and text swift
uibarbuttonitem swift
uibarbuttonitem icon
uibutton right align image
uibutton image size

When I try to use an image for a UIBarButtonItem, the text isn't shown. Is there a way to show both the text and the image?

You can init the UIBarButtonItem with a custom view that has both image and text. Here's a sample that uses a UIButton.

UIImage *chatImage = [UIImage imageNamed:@"08-chat.png"];

UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
[chatButton setBackgroundImage:chatImage forState:UIControlStateNormal];
[chatButton setTitle:@"Chat" forState:UIControlStateNormal];
chatButton.frame = (CGRect) {
    .size.width = 100,
    .size.height = 30,
};

UIBarButtonItem *barButton= [[[UIBarButtonItem alloc] initWithCustomView:chatButton] autorelease];
self.toolbar.items = [NSArray arrayWithObject:barButton];

How can I have a UIBarButtonItem with both image and text?, You can init the UIBarButtonItem with a custom view that has both image and text. Here's a sample that uses a UIButton. UIImage *chatImage = [UIImage  Technically a UIButton is a View since it inherits from UIView, so you can use that as your custom view. Depending on the size of "tools_icon", you may need to make any necessary adjustments, but the width of the button frame must be long enough to contain both the image icon and the text. answered Mar 24 '15 at 4:00

I suggest a way how to do this using Storyboard:

  1. Drag and drop to ToolBar usual UIView. It automatically will be wrapped to Bar Button Item. Adjust View width in "Size inspector". Change background color of View to clear color.

  1. Put UIButton and UILabel inside a View. You can use constraints to adjust their positions. Also you can put thier outside of a View, for example, little higher or less (like on my picture). Put Default and Highlighted images for UIButton.

  2. Copy Bar Button Item and adjust another buttons. Add Flexible spaces.

  3. Create outlets.

  4. Done :). When you run app, you get Tool Bar like this:

P.S. Here you can read how to create UIToolbar subclass using .xib

UIBarButtonItem with Image, Create UIBarButtonItem programmatically; Create UIImage programmatically; Add image to UIBarButtonItem; Add target action to UIBarButtonItem to call a local let leftBarButton = UIBarButtonItem(title: "Edit", style: UIBarButtonItemStyle​.done, AWS Facebook Facebook SDK Facebook Sign in Firebase HTTP HTTP Get  I have an image that I want to display on a UIBarButtonItem, but for some reason it only shows the outline of it and the rest is all white. How can I have it actually display the image?

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;

UIBarButtonItem, class UIBarButtonItem : UIBarItem a value for the normal state to be used by other states which don't have a custom value set. Initializes a new item using the specified image and other properties. init(title: String?, style: UIBarButtonItem. An abstract superclass for items that can be added to a bar that appears at the bottom of the screen. class UIBar Button Item Group A set of bar button items on the shortcuts bar above the keyboard on iPad.

Kris Markel's answer is a good start (only < iOS7), but if you use tint color, it wount work for the image and the highlighted state of the button.

I've created a category that will create a UIBarButtonItem (leftBarButtonItem) that looks and behaves like a normal iOS7 back button. Have a look at: https://github.com/Zero3nna/UIBarButtonItem-DefaultBackButton

UIButton with label text and right aligned image, For some reason it was hard for me to figure out how to achieve the result shown in the picture above. However it turned out to be simple. The padding for the  You can init the UIBarButtonItem with a custom view that has both image and text. Here's a sample that uses a UIButton.

Swift 4.2 with target

extension UIBarButtonItem {
    convenience init(image :UIImage, title :String, target: Any?, action: Selector?) {
        let button = UIButton(type: .custom)
        button.setImage(image, for: .normal)
        button.setTitle(title, for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

        if let target = target, let action = action {
            button.addTarget(target, action: action, for: .touchUpInside)
        }

        self.init(customView: button)
    }
}

iPhone SDK Application Development: Building Applications for the , Both types of buttons are created as UIBarButtonItem objects, however you Initialize image buttons using the initWithImage method, and initialize standard text  So anywhere in your code you can create bar item calling this method (provided that you include a header with its declaration). P.S. You do not need to use 'v' UIView as you can create UIBarButtonItem with a button as custom view directly. P.P.S. You also need [forward release] in your code.

How to add a custom view to a UIBarButtonItem, Most UIBarButtonItems contain either an icon or some text, but they can do so much more – in fact, you can embed any kind of UIView subclass  You can also drag a UIButton onto a toolbar or navigation bar in Interface Builder if you want to create the button with drag-and-drop instead of programmatically. Unfortunately this means creating the button background image yourself. There's no way to customise the font of a standard UIBarButtonItem prior to iOS5.

Navigation Bar Customisation. Part 1, We have our navigation bar witch by default looks like this: and we (or our rightBarButtonItem = UIBarButtonItem( title: "Button", style: .plain, target: self, action: By default this button has both the image and the title. It has a  UIBarButtonItem Image and Text. You can create a UIBarButtonItem with any UIView subclass, so any UIButton or even a view with custom contents will work:

Making a custom back button in iOS without any pain, And let's not even begin to talk the about image stretching we suddenly got in iOS8. There is return UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain​  iOS: How To Easily Customize The Look Of Your UINavigationBar And UIBarButtonItem Across Your Whole App. Posted on September 13th, 2013. Yesterday, I had the fun task of changing the look of the back button across the whole iOS app. In the process, I ended up learning A LOT about customizing the UINavigationBar in general.

Comments
  • For the last line I get an error message saying "Request for member 'toolbar' is something not a structure or a union. How do I do it using self.navigationItem.rightBarButtonItem?
  • In that case, replace the last line with self.navigationItem.rightBarButtonItem = barButton;. Or you can use this method: developer.apple.com/library/ios/documentation/uikit/reference/….
  • [self.navigationItem setRightBarButtonItem:barButton animated:YES];
  • It's not necessary to use a background image - you can just set the title and image of the UIButton and the image will automatically appear to the left of the text.
  • Bill:How to just set title and image?UIImage *image = [[UIImage imageNamed:@"Game.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)]; button.title=@"3333"; It doesn't work.
  • it works. Any idea where the pregnant pauses come from between button press and arrival into the action specified?
  • @AntonTropashko The problem is (1) the custom view makes it slower and (2) this is not the correct way to customize the Back button.
  • Add this line to get rid of the left space: button.imageEdgeInsets = UIEdgeInsets(top: 0, left: -13.0, bottom: 0, right: 0)