8

我有一个 UIToolBar 按钮声明如下:

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

我还有一个 UIBarButtonItem 作为后退按钮添加到左侧。但是我如何在 UIToolbar 中添加一个居中的标签标题呢?

4

6 回答 6

7

创建一个 UILabel 并将其添加为工具栏的一项:

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

如果要居中,请在两侧添加灵活的空间:

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
于 2013-10-21T08:21:02.883 回答
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]];

我认为这将解决您的问题。

于 2013-10-21T08:21:18.467 回答
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): 带有自定义视图的 UIBarButtonItem 中的 UILabel

于 2016-01-26T21:33:23.560 回答
1

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

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

于 2013-10-21T08:26:15.397 回答
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()
于 2017-10-02T20:14:55.507 回答
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)
于 2016-07-07T16:50:26.757 回答