2012-04-19 45 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;