2012-04-19 71 views
1

我有一個UINavigationBar並設置了leftBarButtonItem和rightBarButtonItem。該leftbar設置爲:在UINavigationBar中對齊UILabel

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal]; 
     UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease]; 
     self.navigationItem.leftBarButtonItem = myButton; 

和rightbarbutton設置爲:

UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
UIImage *doneButtonImage = nil; 
     doneButtonImage = [UIImage imageNamed:@"done.png"]; 
     [doneBtn setImage:doneButtonImage forState:UIControlStateNormal]; 
    doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn]; 
    self.navigationItem.rightBarButtonItem = doneBarButtonItem; 

回來的寬度爲23像素,做是72px,所以他們不相等。現在問題是我有一個標籤,我希望始終處於中心位置。如果標籤中的文本太長,會干擾正確的欄按鈕,我想剪切文本。這裏是我如何設置它:

titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    titleLabel.text = self.pageTitle; // Can't change 
    titleLabel.adjustsFontSizeToFitWidth = YES; 
    titleLabel.clipsToBounds = YES; 
    titleLabel.numberOfLines = 1; 
    titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
    titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0]; 
    titleLabel.backgroundColor = [UIColor yellowColor]; 
    titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
    titleLabel.textAlignment = UITextAlignmentCenter; 
    [titleLabel sizeToFit]; 
    titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight; 


self.navigationItem.titleView = titleLabel; 

但是,這不會中心標題..我該怎麼做?

+0

只是手動設置的x值框架,以使它看起來集中於你。 – 2012-04-19 06:09:36

+0

或者改爲使用工具欄並在所有內容之間添加靈活的空間項目 – Otium 2012-04-19 06:14:30

回答

1

我建議先算self.pageTitle 的寬度,則相應地設置titleLabel.I的位置更喜歡這個: -

titleLabel = [[UILabel alloc] init]; 
titleLabel.text = self.pageTitle; // Can't change 

**CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have. 
CGSize expectedLabelSize = [titleLabel.text titleLabel.font 
              constrainedToSize:maximumTitleLabelSize 
               lineBreakMode:UILineBreakModeWordWrap]; 
//adjust the label the the new width. 
CGRect newFrame = titleLabel.frame; 
newFrame.size.width = expectedLabelSize.width; 
titleLabel.size.width = newFrame.size.width;**  
if(titleLabel.size.width==100) 
{ 
titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30); 
} 
else if(titleLabel.size.width==200) 
{ 
titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30); 
} 

titleLabel.adjustsFontSizeToFitWidth = YES; 
titleLabel.clipsToBounds = YES; 
titleLabel.numberOfLines = 1; 
titleLabel.font = [UIFont fontWithName:@"PTSans-Narrow" size:30.0]; 
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0]; 
titleLabel.backgroundColor = [UIColor yellowColor]; 
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight; 
titleLabel.textAlignment = UITextAlignmentCenter; 
[titleLabel sizeToFit]; 
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight; 

self.navigationItem.titleView = titleLabel; 

嘗試this.It將有助於周到,謝謝:)

0

請勿使用NavigationItem添加左右欄按鈕,而應使用addsubview方法爲導航欄添加自定義按鈕。

0

使用工具欄添加多個按鈕到你的導航欄。

// create a toolbar to have two buttons in the right 
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)]; 

// create the array to hold the buttons, which then gets added to the toolbar 
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3]; 

// create a standard "add" button 
UIBarButtonItem* bi = [[UIBarButtonItem alloc]`enter code here` 
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// create a spacer 
bi = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
[buttons addObject:bi]; 
[bi release]; 

// create a standard "refresh" button 
bi = [[UIBarButtonItem alloc] 
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refresh:)]; 
bi.style = UIBarButtonItemStyleBordered; 
[buttons addObject:bi]; 
[bi release]; 

// stick the buttons in the toolbar 
[tools setItems:buttons animated:NO]; 

[buttons release]; 

// and put the toolbar in the nav bar 
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools]; 
[tools release]; 
0

你只需使用此代碼它做工精細嘗試

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 190, 30)]; 
titleLabel.textColor = [UIColor whiteColor]; 
titleLabel.textAlignment = UITextAlignmentCenter; 
titleLabel.text [email protected]"My Wish List"; 
titleLabel.backgroundColor = [UIColor clearColor]; 
titleLabel.font = [UIFont fontWithName:@"Copperplate" size:16.0]; 
titleLabel.minimumFontSize = 10; 
titleLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth; 
self.navigationItem.titleView = titleLabel; 
[titleLabel release]; 

我用這個代碼在我的項目也....... :-)

1

這是最簡單的解決方案,將它添加到titleView之前使用sizeToFit方法:

UILabel *titleLabel = [[UILabel alloc] init]; 
titleLabel.text = @"My Title"; 
titleLabel.font = [UIFont boldSystemFontOfSize:17.0f]; 
titleLabel.textColor = [UIColor whiteColor]; 
[titleLabel sizeToFit]; 

self.navigationItem.titleView = titleLabel;