I have implemented Custom input accessory view it was working fine till iOS 10.3.1. But it's not visible in iOS 11 beta.
Have anyone experience this issue?
The question you ask does not have much detail. But I had the same problem when using an inputAccessoryView and a custom inputView for the textfield.
And resolved this on iOS11 by setting the custom inputView's autoresizingMask to .flexibleHeight.
yourCustomInputView.autoresizingMask = .flexibleHeight
Hope this resolves the issue. If not maybe provide some more information?
Here is how I add the input accessory, incase this is of more help (as extension of textfield):
public extension UITextField {
public func addToolbarInputAccessoryView(barButtonItems: [UIBarButtonItem],
textColour: UIColor,
toolbarHeight: CGFloat = 44,
backgroundColour: UIColor = .white) {
let toolbar = UIToolbar()
toolbar.frame = CGRect(x: 0, y: 0, width: bounds.width, height: toolbarHeight)
toolbar.items = barButtonItems
toolbar.isTranslucent = false
toolbar.barTintColor = backgroundColour
toolbar.tintColor = textColour
inputAccessoryView = toolbar
}
}
And then on the inputView (not the inputAccessoryView), I was using a date picker for example - just make sure that the date picker's autoresizing mask is set to flexible height.
PSA: If you use a UIToolbar as your custom view, it's currently broken in iOS 11 GM.
Instead of loosing your hair on how to fix it, just change it to UIView.
You'll loose the blur effect but it will work.
Beta 3 has just come out and some people said it solved the problem, but for me it didn't.
However I tried setting the accessory view to something stupid (100pxls high) and spotted that the Undo/Redo/Paste bar on the iPads was incorrectly sitting over the top of my accessory bar.
So I added the following code to get rid of Apples bar (it was pointless for my custom picker anyway) and the problem went away
Hope this helps somebody
- (void)textFieldDidBeginEditing:(UITextField*)textField
{
UITextInputAssistantItem* item = [textField inputAssistantItem];
item.leadingBarButtonGroups = #[];
item.trailingBarButtonGroups = #[];
}
To avoid the inputAccessoryView issue in iOS 11 for UITextField and UITextView, just use the following code:
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
self.monthPickerView.backgroundColor = [UIColor whiteColor];
self.monthPickerView.delegate = self;
self.monthPickerView.dataSource = self;
[inputView addSubview:self.monthPickerView];
cell.monthTextField.inputView = inputView ;
self.monthTextField.inputAccessoryView = [self doneButtonAccessoryView];
// doneButtonAccessoryView Method
-(UIToolbar*)doneButtonAccessoryView
{
UIToolbar *kbToolbar = [[UIToolbar alloc] init];
[kbToolbar sizeToFit];
[kbToolbar setBarTintColor:[UIColor whiteColor]];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(doneClicked)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleDone target:self
action:#selector(cancelClicked)];
NSDictionary *attrDict;
attrDict = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"Helvetica-Bold" size:16.0], NSFontAttributeName, nil];
[doneButton setTitleTextAttributes:attrDict forState:UIControlStateNormal];
UIBarButtonItem *flexWidth = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
[kbToolbar setItems:[NSArray arrayWithObjects:cancelButton,flexWidth, doneButton, nil]];
return kbToolbar;
}
UIToolBar is broken in iOS 11. But you can get the same thing done using UIView as inputAccessoryView. Sample code snippet here:
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
UIView* toolBar = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f, width, 44.0f)];
toolBar.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.0f];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0 , 0.0f, width, 44.0f)];
[titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:13]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextColor:[UIColor redColor]];
[titleLabel setText:#"Title"];
[titleLabel setTextAlignment:NSTextAlignmentLeft];
[toolBar addSubview:titleLabel];
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[doneBtn setTitle:#"Done" forState:UIControlStateNormal];
doneBtn.tintColor = [UIColor colorWithRed:(float)179/255 green:(float)27/255 blue:(float)163/255 alpha:1];
[doneBtn.titleLabel setFont:[UIFont fontWithName:#"Helvetica" size:16]];
[doneBtn addTarget:self action:#selector(btnTxtDoneAction) forControlEvents:UIControlEventTouchUpInside];
[doneBtn setFrame:CGRectMake(width-70, 6, 50, 32)];
[toolBar addSubview:doneBtn];
[toolBar sizeToFit];
txtMessageView.inputAccessoryView = toolBar;
Hope this help..:)
I've had the same issue and I've found out that removing all of the bottom, top, leading, training, left, right constraints for the
view that is assigned accessoryView solved it.
Swift 4 solution
let toolBarRect = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 44)
let toolBar = UIView(frame: toolBarRect)
toolBar.backgroundColor = .lightGray
let nextButton = UIButton()
nextButton.setTitleColor(.black, for: .normal)
nextButton.setTitle("Next", for: .normal)
nextButton.addTarget(self, action: #selector(self.onNextButtonTouch), for: .touchUpInside)
nextButton.translatesAutoresizingMaskIntoConstraints = false
toolBar.addSubview(nextButton)
NSLayoutConstraint.activate(
[
nextButton.heightAnchor.constraint(equalToConstant: Constants.keyboardToolBarHeight),
nextButton.trailingAnchor.constraint(equalTo: toolBar.trailingAnchor, constant: -16),
nextButton.centerYAnchor.constraint(equalTo: toolBar.centerYAnchor, constant: 0)
]
)
self.yourTextField.inputAccessoryView = toolBar
Just in case someone might still need the solution, here's what I did (IOS 12.1);
private func initSearchBox() {
// Add Done button on keyboard
txtSearch.delegate = self
let tbrDone = UIToolbar()
let btnDone = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(btnDone_tapped))
tbrDone.items = [btnDone]
tbrDone.sizeToFit()
self.txtSearch.inputAccessoryView = tbrDone
}
#objc func btnDone_tapped() {
view.endEditing(true)
}
Related
how do I reduce the distance between the two items?
my code:
-(void)setupRightMenuButton{
filtro = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon-filtro-bar"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFilter)];
busca = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"icon-busca"] style:UIBarButtonItemStylePlain target:self action:#selector(moveToSearchView)];
filtro.tintColor = [UIColor whiteColor];
busca.tintColor = [UIColor whiteColor];
filtro.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
busca.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:busca, filtro, nil] animated:YES];
}
My viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupLeftMenuButton];
[self.navigationController.navigationBar setBarTintColor: [UIColor colorWithRed:0.49 green:0.075 blue:0.082 alpha:1]]; /*#7d1315*/
[self.navigationController.navigationBar setTitleTextAttributes:#{NSForegroundColorAttributeName : [UIColor whiteColor]}];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)
forBarMetrics:UIBarMetricsDefault];
[self setupRightMenuButton];
self.title = #"Músicas";
}
I tried using the filtro.imageInsets = UIEdgeInsetsMake (6, 0, -6, 0); but it did not work
Try to add buttons this way:
UIButton *but1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
UIView *but1View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[but1View addSubview:but1];
UIBarButtonItem *rightButton1 = [[UIBarButtonItem alloc] initWithCustomView:but1View];
UIButton *but2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, width, height)];
UIView *but2View = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[but2View addSubview:but2];
UIBarButtonItem *rightButton2 = [[UIBarButtonItem alloc] initWithCustomView:but2View];
self.navigationItem.rightBarButtonItems=#[ rightButton,rightButton2];
you need to mention the postions of x and y for the both UIViews as where ou want your buttons on the nav bar.
This code works for me. Try to create a button, then set the image to it and set the buttons frame.
let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
let image = UIImage(named: "your_image")!
button.setImage(image , forState: UIControlState.Normal)
// optional -------------
button.addTarget(self, action: "your_function", forControlEvents: UIControlEvents.TouchUpInside)
// ------------
button.frame = CGRectMake(0, 0, image.width, image.height)
let barButton = UIBarButtonItem(customView: button)
Create as many barButtons as you want and then add them to navigationItem:
self.navigationItem.setRightBarButtonItems([barBtn1, barBtn2], animated: false)
What is the proper way to change height of navigation bar?
I need to create custom title in navigation bar, it should contains two UILabels one above other. The Title should be resized to fit those UILabels.
Should I override sizeThatFits: method in my custom TitleView, would other buttons correctly change to fit that size? How can I change a size of NavigationBar?
That is what I need to create using latest SDK features.
Create the following class category (you can create it in your implementation file):
#import "objc/runtime.h"
#interface UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height;
#end
static char const *const kHeight = "Height";
#implementation UINavigationBar (CustomHeight)
- (void)setHeight:(CGFloat)height
{
objc_setAssociatedObject(self, kHeight, #(height), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)height
{
return objc_getAssociatedObject(self, kHeight);
}
- (CGSize)sizeThatFits:(CGSize)size
{
CGSize newSize;
if (self.height) {
newSize = CGSizeMake(self.superview.bounds.size.width, [self.height floatValue]);
} else {
newSize = [super sizeThatFits:size];
}
return newSize;
}
#end
And after that, simply call [self.navigationController.navigationBar setHeight:100.0] in your viewDidLoad or where you need it. Works in both iOS 7.1 and 8.1.
Disclaimer: Any alteration of the API and its functions is prone to future issues with new OS releases! Apple does not intend to allow us to change the navigation bar height (except for some rare instances) so use any solution wisely after assessing the risks vs advantages.
Looking at that design that you posted, I don't have the impression that the bar is really higher than the standard 44pt. Please note that starting with iOS7 the status bar is integrated into the same bar and the 20pt of the status bar is added to the total height.
IMHO, the only problem you need to solve is to stack the title/subtitle, and that can be easily done with a custom titleView, as Kampai has demonstrated.
Add custom view for both navigation title and navigation right bar button.
Here how you can add title view like above.
// Custom view for navigation title.
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 22)];
label1.text = #"Stasik";
label1.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:20];
label1.textAlignment = NSTextAlignmentCenter;
[customTitleView addSubview:label1];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 22, 100, 22)];
label2.text = #"Stasik iPhone";
label2.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:16];
label2.textAlignment = NSTextAlignmentCenter;
label2.textColor = [UIColor darkGrayColor];
[customTitleView addSubview:label2];
self.navigationItem.titleView = customTitleView;
// Custom view for right navigation.
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeCustom];
upButton.frame = CGRectMake(0, 0, 20, 44);
[upButton setImage:[UIImage imageNamed:#"up"] forState:UIControlStateNormal];
[upButton addTarget:self action:#selector(upButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *upButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:upButton];
UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom];
downButton.frame = CGRectMake(0, 0, 20, 44);
[downButton setImage:[UIImage imageNamed:#"down"] forState:UIControlStateNormal];
[downButton addTarget:self action:#selector(downButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *downButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:downButton];
// Remove trailing space for right view.
UIBarButtonItem *nagativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
nagativeSpace.width = -11;
self.navigationItem.rightBarButtonItems = #[nagativeSpace, upButtonBarItem, downButtonBarItem];
// Left item
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 40, 44);
[backButton setImage:[UIImage imageNamed:#"back"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
// Remove leading space for left view.
nagativeSpace.width = -15;
UIBarButtonItem *backButtonBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItems = #[nagativeSpace, backButtonBarItem];
OutPut:
Arrow images are different than OP's requirement but it serves layout for navigation views.
From above code make change in nagativeSpace.width value to arrange buttons with accurate distance from left as well as right margins.
Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.
Quora's iOS 7 app seemingly displays a UISearchBar in the main UINavigationController's UINavigationBar. Search is triggered by tapping on the right bar button item in that bar and animates in from the right. Everything appears to happen in a single view controller.
Apple introduced the "displaysSearchBarInNavigationBar" property to UISearchDisplayController in iOS 7, but I don't believe that Quora have used this. It's not possible to have a titleView in the navigation bar when set and the Quora app certainly appears to have this.
Apple's Calendar app uses a separate view controller for search, but keeping search in the same view controller as the content, as Quora do, is a nicer experience for the user.
The only way I've thought to do this might be to use custom UIViewController transitions, but that feels like an overkill. Is there a simpler way to create this?
Here's a quick sample view controller I wrote to mimic the Quora "expanding search field" effect. I'll leave it up to you to incorporate a "search results" tableview.
I used a UITextField (subclassed so I could change the color of the placeholder text...), but you MIGHT be able to use a UISearchBar. Or you might want to make a custom view that contains the UITextField and any "close" button. In my example I used the UITextField rightView to hold a close button; in the Quora case they have the close button outside the text field, like a real UISearchBar. But I don't think you can can change the text/image of the close button on a UISearchBar (at least not easily).
It's possible that you could come up with a solution that cleanly integrated the built-in searchViewController, but it might be too much trouble for its worth.
#interface TSTextField : UITextField
#end
#implementation TSTextField
- (void) drawPlaceholderInRect: (CGRect) rect
{
CGFloat fontHeight = self.font.lineHeight;
CGFloat yOffset = (rect.size.height - fontHeight) / 2.0;
rect = CGRectMake( 0, yOffset, rect.size.width, fontHeight );
[[self placeholder] drawInRect: rect
withAttributes: #{ NSFontAttributeName : self.font,
NSForegroundColorAttributeName : self.isEditing ? self.textColor : [UIColor whiteColor] }];
}
#end
#interface TSViewController () <UITextFieldDelegate>
#end
#implementation TSViewController
- (void) viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
UITextField* searchField = [[TSTextField alloc] initWithFrame: CGRectMake(0, 0, 85, 30)];
searchField.backgroundColor = [UIColor clearColor];
searchField.borderStyle = UITextBorderStyleNone;
searchField.textColor = [UIColor whiteColor];
searchField.textAlignment = NSTextAlignmentRight;
searchField.placeholder = #"Search";
searchField.delegate = self;
UIButton* magnifyButton = [UIButton buttonWithType: UIButtonTypeSystem];
[magnifyButton setTitle: #"🔍" forState: UIControlStateNormal];
[magnifyButton sizeToFit];
[magnifyButton addTarget: self action: #selector( close: ) forControlEvents: UIControlEventTouchUpInside];
searchField.leftView = magnifyButton;
searchField.leftViewMode = UITextFieldViewModeAlways;
UIButton* closeButton = [UIButton buttonWithType: UIButtonTypeSystem];
[closeButton setTitle: #"ⓧ" forState: UIControlStateNormal];
[closeButton sizeToFit];
[closeButton addTarget: self action: #selector( close: ) forControlEvents: UIControlEventTouchUpInside];
searchField.rightView = closeButton;
searchField.rightViewMode = UITextFieldViewModeWhileEditing;
UIBarButtonItem* bbi = [[UIBarButtonItem alloc] initWithCustomView: searchField];
self.navigationItem.rightBarButtonItem = bbi;
}
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
UITextField* searchField = (UITextField*)self.navigationItem.rightBarButtonItem.customView;
searchField.borderStyle = UITextBorderStyleRoundedRect;
searchField.backgroundColor = [UIColor whiteColor];
searchField.textColor = [UIColor blackColor];
searchField.text = #"";
searchField.textAlignment = NSTextAlignmentLeft;
[UIView transitionWithView: searchField
duration: 0.25
options: UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchField.frame = CGRectMake( 0, 0, 290, searchField.frame.size.height);
}
completion: nil];
return YES;
}
- (void) close: (id) sender
{
UITextField* searchField = (UITextField*)self.navigationItem.rightBarButtonItem.customView;
searchField.rightViewMode = UITextFieldViewModeNever;
[UIView transitionWithView: searchField
duration: 0.25
options: UIViewAnimationOptionAllowAnimatedContent | UIViewAnimationOptionTransitionCrossDissolve
animations:^{
searchField.frame = CGRectMake( 0, 0, 85, searchField.frame.size.height);
searchField.backgroundColor = [UIColor clearColor];
searchField.text = #"";
searchField.borderStyle = UITextBorderStyleNone;
searchField.textColor = [UIColor whiteColor];
searchField.textAlignment = NSTextAlignmentRight;
[searchField resignFirstResponder];
}
completion:^(BOOL finished) {
searchField.rightViewMode = UITextFieldViewModeWhileEditing;
}];
}
#end
I have a UIToolBar button declared below:
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);
I also have a UIBarButtonItem that gets added in on the left as a back button. But how do I just add a centered label title to the UIToolbar?
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 20)];
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor blackColor];
lblTitle.textAlignment = NSTextAlignmentLeft;
[self.view addSubview:lblTitle];
UIBarButtonItem *typeField = [[UIBarButtonItem alloc] initWithCustomView:lblTitle];
toolBar.items = [NSArray arrayWithArray:[NSArray arrayWithObjects:backButton,spaceBar,lblTitle, nil]];
This will solve your problem i think.
Create a UILabel and add it as an item of the toolbar:
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel];
If you want to center it, add flexible spaces on the sides:
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
None of the other answers were as copy and pasteable as I wanted, so...
UILabel *toolbarLabel = [[UILabel alloc] initWithFrame:CGRectZero];
toolbarLabel.text = #"Text in yo toolbar";
[toolbarLabel sizeToFit];
toolbarLabel.backgroundColor = [UIColor clearColor];
toolbarLabel.textColor = [UIColor grayColor];
toolbarLabel.textAlignment = NSTextAlignmentCenter;
UIBarButtonItem *labelItem = [[UIBarButtonItem alloc] initWithCustomView:toolbarLabel];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:#[flexible, labelItem, flexible] animated:false];
This assumes you have the toolbar showing through your navigation controller (to show it from any View Controller that's shown by a navigation controller, [self.navigationController setToolbarHidden:NO animated:YES];)
It looks something like this (iOS 9):
UIToolbar doesn't have a title property. As others suggested you need to add a UILabel as a title.
Or you can use UINavigationBar instead and initialize using - (id)initWithTitle:(NSString *)title;
Here is a Swift 2.2 implementation of Aaron Ash's answer
let toolbarLabel = UILabel(frame: CGRectZero)
toolbarLabel.text = "Text in yo toolbar"
toolbarLabel.sizeToFit()
toolbarLabel.backgroundColor = UIColor.clearColor()
toolbarLabel.textColor = UIColor.grayColor()
toolbarLabel.textAlignment = .Center
let labelItem = UIBarButtonItem.init(customView: toolbarLabel)
let flexible = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target:nil, action:nil)
self.setToolbarItems([flexible, labelItem, flexible], animated: false)
Here is a Swift 3 implementation:
let topBarTitleLabel = UILabel.init(frame: (CGRect.init(origin: CGPoint.init(x: 0.0, y: 0.0), size: CGSize.init(width: 0.0, height: 0.0))))
topBarTitleLabel.text = "Text in yo toolbar"
topBarTitleLabel.sizeToFit()
topBarTitleLabel.backgroundColor = UIColor.clear
topBarTitleLabel.textColor = UIColor.black
topBarTitleLabel.textAlignment = NSTextAlignment.center
let topBarButtonItemTitleLabel = UIBarButtonItem.init(customView: topBarTitleLabel)
let flexibleBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
self.topToolBar.setItems([flexibleBarButtonItem, topBarButtonItemTitleLabel, flexibleBarButtonItem], animated: false)
self.topToolBar.setNeedsLayout()