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 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]; … Read more

How to set image for bar button with swift?

I have achieved that programatically with this code: import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() //create a new button let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton //set image for button button.setImage(UIImage(named: “fb.png”), forState: UIControlState.Normal) //add function for button button.addTarget(self, action: “fbButtonPressed”, forControlEvents: UIControlEvents.TouchUpInside) //set frame button.frame = CGRectMake(0, 0, 53, 31) … Read more

Change position of UIBarButtonItem in UINavigationBar

This code creates a back button for UINavigationBar with image background and custom position. The trick is to create an intermediate view and modify its bounds. Swift 5 let menuBtn = UIButton(type: .custom) let backBtnImage = UIImage(named: “menu”) menuBtn.setBackgroundImage(backBtnImage, for: .normal) menuBtn.addTarget(self, action: #selector(showMenuTapped), for: .touchUpInside) menuBtn.frame = CGRect(x: 0, y: 0, width: 45, height: … Read more

How do I show/hide a UIBarButtonItem?

Save your button in a strong outlet (let’s call it myButton) and do this to add/remove it: // Get the reference to the current toolbar buttons NSMutableArray *toolbarButtons = [self.toolbarItems mutableCopy]; // This is how you remove the button from the toolbar and animate it [toolbarButtons removeObject:self.myButton]; [self setToolbarItems:toolbarButtons animated:YES]; // This is how you … Read more

Adding a UILabel to a UIToolbar

Have a look into this [[UIBarButtonItem alloc] initWithCustomView:yourCustomView]; Essentially every item must be a “button” but they can be instantiated with any view you require. Here is some example code. Note, since other buttons are typically on the toolbar, spacers are placed on each side of the title button so it stays centered. NSMutableArray *items … Read more

UIBarButtonItem in navigation bar programmatically?

Custom button image without setting button frame: You can use init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?) to initializes a new item using the specified image and other properties. let button1 = UIBarButtonItem(image: UIImage(named: “imagename”), style: .plain, target: self, action: Selector(“action”)) // action:#selector(Class.MethodName) for swift 3 self.navigationItem.rightBarButtonItem = button1 Check this Apple Doc. reference … Read more