Setting header in UIToolBar on iOS 7

I have a UIToolBar button 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 is added on the left as the back button. But how to add a centered-label shortcut to UIToolbar?

+6
source share
6 answers
 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.

+7
source

Create a UILabel and add it as a toolbar item:

 UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel]; 

If you want to center it, add flexible spaces around the sides:

 UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] 
+6
source

None of the other answers were as copied and pasted 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 that the toolbar is displayed through your navigation controller (to show it from any view controller displayed by the navigation controller, [self.navigationController setToolbarHidden:NO animated:YES]; )

It looks something like this (iOS 9): UILabel in UIBarButtonItem with custom view

+4
source

UIToolbar does not have a title property. Like others, you need to add UILabel as your title.

Or you can use the UINavigationBar and initialize with - (id)initWithTitle:(NSString *)title;

+1
source

Here is a quick version 2.2 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) 
+1
source

Here is the 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() 
0
source

Source: https://habr.com/ru/post/956387/


All Articles