【问题标题】:centering UILabel in UINavigationBar在 UINavigationBar 中居中 UILabel
【发布时间】:2012-04-19 06:03:14
【问题描述】:

我有一个 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;

back 的宽度是 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;

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

【问题讨论】:

  • 只需手动将框架中的 x 值设置为使其看起来居中的任何值。
  • 或者使用工具栏并在所有内容之间添加灵活的空格项

标签: iphone objective-c ios ipad uinavigationbar


【解决方案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;

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

【讨论】:

    【解决方案2】:

    这是最简单的解决方案,在添加到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;
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        使用工具栏为您的导航栏添加多个按钮。

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

        【讨论】:

          【解决方案5】:

          您只需使用此代码,它的工作正常尝试一下

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-07-16
            • 2015-05-07
            • 2016-04-11
            • 2015-05-22
            • 1970-01-01
            • 1970-01-01
            • 2018-10-10
            相关资源
            最近更新 更多