我们如何在导航栏中的UIBarButtonItem中放置两行

时间:2012-01-19 13:22:54

标签: xcode uinavigationbar uibarbuttonitem navigationbar

“正在播放”在UIBarButtonItem的一行中。我必须把它分成两行,比如“Now”是ay top而“Playing”就在底部。我写了以下代码行: -

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]

                              initWithTitle:@"Now Playing" 

                              style:UIBarButtonItemStyleBordered 

                              target:self 

                              action:@selector(flipView)];


    self.navigationItem.rightBarButtonItem = flipButton;  

所以我想在“现在正在播放”之间打断。所以请帮帮我。

4 个答案:

答案 0 :(得分:9)

是的,你可以。这很简单。创建一个多行按钮并使用它。 “技巧”是设置titleLabel numberOfLines属性,使其喜欢多行。

UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.numberOfLines = 0;
[button setTitle:NSLocalizedString(@"Now\nPlaying", nil) forState:UIControlStateNormal];
[button sizeToFit];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

当你指定一个自定义的按钮类型时,它希望你配置一切......选择状态等,这里没有显示,以使答案集中在手头的问题上。

答案 1 :(得分:4)

- (void)createCustomRightBarButton_
{
    UILabel * addCustomLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 64, 25)] autorelease];
    addCustomLabel.text =@"Now\nPlaying";
    addCustomLabel.textColor = [UIColor whiteColor];
    addCustomLabel.font = [UIFont boldSystemFontOfSize:11];
    addCustomLabel.numberOfLines = 2;
    addCustomLabel.backgroundColor = [UIColor clearColor];
    addCustomLabel.textAlignment = UITextAlignmentCenter;

    CGSize size = addCustomLabel.bounds.size;

    UIGraphicsBeginImageContext(size);      
    CGContextRef context = UIGraphicsGetCurrentContext();       
    [addCustomLabel.layer renderInContext: context];
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage * img = [UIImage imageWithCGImage: imageRef];
    CGImageRelease(imageRef);
    CGContextRelease(context);

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:img
                                                                           style:UIBarButtonItemStyleBordered 
                                                                          target:self 
                                                                          action:@selector(flipView)]
                                              autorelease];
}

答案 2 :(得分:0)

您可以在条形按钮内托管UIButton作为customView(可以设置条形按钮项目customView,也可以直接在UIBarButtonItem上拖动一条),将其作为插座连接,然后执行;

-(void) viewDidLoad
{
  //...
  self.customButton.titleLabel.numberOfLines = 2;
  self.customButton.suggestionsButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
  self.customButton.suggestionsButton.titleLabel.textAlignment = UITextAlignmentCenter;
}

在我的情况下成为

enter image description here

答案 3 :(得分:0)

enter image description here enter image description here

我自己制作2张PNG图像,看起来不错。

UIImage *img = [UIImage imageNamed:@"nowplaying.png"];
UIBarButtonItem *nowPlayingButtonItem = [[[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleBordered target:delegate action:@selector(presentNowPlayingMovie)] autorelease];