1

我有一个 UINavigationBar 并设置了 leftBarButtonItem 和 rightBarButtonItem。左边栏设置为:

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;

背面的宽度是 23px,done 是 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;

但是,这不会使标题居中。我该怎么做?

4

5 回答 5

1

我建议先计算 self.pageTitle 的宽度,然后相应地设置 titleLabel 的位置。我更喜欢这个:-

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;

试试这个。它会帮助你。谢谢:)

于 2012-04-19T06:52:55.877 回答
1

这是最简单的解决方案,sizeToFit在将其添加到之前使用该方法titleView

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;
于 2014-06-06T04:50:47.400 回答
0

不要使用 NavigationItem 添加左右栏按钮,而是使用 addsubview 方法为导航栏添加自定义按钮。

于 2012-04-19T06:50:14.507 回答
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];
于 2012-04-19T06:51:59.133 回答
0

您只需使用此代码即可正常工作尝试一下

UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 190, 30)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text =@"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];

我也在我的项目中使用此代码....... :-)

于 2012-04-21T14:27:28.947 回答