2013-10-21 64 views
6

設置一個標題爲UIToolBar我有一個UIToolBar按鈕下方宣稱:在IOS 7

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

我也有被添加在左邊的返回按鈕的UIBarButtonItem一個。但是,我怎樣才能將一個居中的標籤標題添加到UIToolbar?

回答

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

這將解決你的問題,我想。

6

創建一個UILabel並將其添加爲工具欄的項目:

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

如果要居中,在兩側加上靈活的空間:

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

我有這樣的標籤:*的UILabel errorLabel = [[ALLOC的UILabel] initWithFrame:方法CGRectMake(100.0,100.0,140.0,125.0)];但矩形的y屬性不起作用 – cdub

+0

您的工具欄是64高度,如果您的標籤從100開始,則需要更改y。 –

+0

是的,但UILabel文本與我的UIBarButtonItem後退按鈕相比是關閉的。如何將頁面上的UILabel文本向下移動 – cdub

1

UIToolbar沒有一個title屬性。正如其他建議則需要添加一個的UILabel作爲標題。

或者你可以使用UINavigationBar的,而不是使用- (id)initWithTitle:(NSString *)title;

4

沒有其他的答案是複製和pasteable因爲我想的初始化,所以...

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

+0

很好的答案,謝謝。給你10分獎金(在其他答案上)。 –

1

這裏是一個雨燕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) 
0

下面是一個斯威夫特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()