在IOS 7中为UIToolBar设置标题

8

我有一个以下声明的UIToolBar按钮:

UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 64.0);

我还有一个UIBarButtonItem,在左边作为返回按钮添加。但是,我如何只添加一个居中的标签标题到UIToolbar中?

6个回答

7
创建一个UILabel并将其添加为工具栏的项目:
UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:myLabel];

如果您想将其居中,可以在两侧添加灵活的空格:

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

我有这个标签:UILabel *errorLabel = [[UILabel alloc] initWithFrame:CGRectMake(100.0, 100.0, 140.0, 125.0)]; 但是矩形的y属性没有正常工作。 - cdub
你的工具栏高度为64,而你的标签从100开始,你需要改变y值。 - Antonio MG
是的,但我的UILabel文本与UIBarButtonItem返回按钮相比有偏差。我该如何将UILabel文本向下移动页面? - cdub
可以加一些 y,但不要到 100!那会超出工具栏。 - Antonio MG

7
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]];

我想这会解决你的问题。

4

其他答案都不如我想要的那样容易复制和粘贴,所以...

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];

这假设你的导航控制器中有工具栏(为了在由导航控制器显示的任何视图控制器中显示它,[self.navigationController setToolbarHidden:NO animated:YES];)。
它看起来像这样(iOS 9):UILabel in a UIBarButtonItem with a custom view

好的回答,谢谢。我在另一个回答中给了你10分的奖励。 - meaning-matters

1
这是Aaron Ash回答的Swift 2.2实现。
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
这是一个Swift 3实现:

    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()

1

UIToolbar没有标题属性。正如其他人建议的那样,您需要添加一个UILabel作为标题。

或者您可以使用UINavigationBar,并使用- (id)initWithTitle:(NSString *)title;进行初始化。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接