2013-04-01 67 views
0

我想知道我怎麼能擺脫空間的左側和右側從的UIBarButtonItem兩行標籤,它有鑑於setCustomView設置如下:的UIBarButtonItem寬度與兩行標籤

UIButton* forwardButton = [UIButton buttonWithType:101]; 

[forwardButton setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 
[forwardButton.titleLabel setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 

[forwardButton setTitle:@"Now\nPlaying" forState:UIControlStateNormal]; 
[forwardButton addTarget:self action:@selector(openPlayer) forControlEvents:UIControlEventTouchUpInside]; 

[forwardButton.titleLabel setNumberOfLines:2]; 
[forwardButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
[forwardButton.titleLabel setTextAlignment:NSTextAlignmentCenter]; 
[forwardButton.titleLabel setFont:[UIFont boldSystemFontOfSize:10]]; 

[navButton setCustomView:forwardButton]; 

Here是結果我得到。

按鈕的寬度是因爲如果標籤已經文本寫在一行像this

回答

1

你可以嘗試改變邊界的按鈕。

UIButton* forwardButton = [UIButton buttonWithType:101]; 
[forwardButton setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 
[forwardButton.titleLabel setTransform:CGAffineTransformMakeScale(-1.0, 1.0)]; 

[forwardButton setTitle:@"Now\nPlaying" forState:UIControlStateNormal]; 
[forwardButton.titleLabel setNumberOfLines:2]; 
[forwardButton.titleLabel setLineBreakMode:NSLineBreakByWordWrapping]; 
[forwardButton.titleLabel setTextAlignment:NSTextAlignmentCenter]; 
[forwardButton.titleLabel setFont:[UIFont boldSystemFontOfSize:10]]; 

forwardButton.bounds = CGRectMake(0, 0, 30, 30); 


UIBarButtonItem* forwardButtonItem = [[UIBarButtonItem alloc] initWithCustomView: forwardButton]; 
self.navigationItem.rightBarButtonItem = forwardButtonItem; 

試用了

+0

感謝您的回答工作,但不,這並不能改變什麼 – AlimovAndrei

+0

感謝,很靈的,我試過設置標題= ___ =」 – AlimovAndrei

+0

前設置邊界還有一個問題出現了。該解決方案僅適用於導航欄樣式設置爲默認值,如果我嘗試黑色寬度的問題再次出現。試過之前和之後的解決方案代碼設置風格,以及在界面生成器設置它。 – AlimovAndrei

0
NSString *[email protected]"Now\Playing"; 
BarButton.titleLabel setLineBreakMode:UILineBreakModeWordWrap]; 

[BarButton setTitle:titleStr forState:UIControlStateNormal]; 
+0

感謝,但沒有更改 – AlimovAndrei

+0

@ AlimovAndrei-使用BarButton.titleLabel.numberOfLines = 0 – iPatel

+0

@ AlimovAndrei- http://stackoverflow.com/questions/8926732/how-can-we-put-two-line-in-uibarbuttonitem-in-navigation-bar – iPatel