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

9 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;  
Run Code Online (Sandbox Code Playgroud)

所以我想在"现在正在播放"之间进行打破.所以请帮帮我.

Ray*_*Fix 10

是的你可以.这很简单.创建一个多行按钮并使用它."技巧"是设置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];
Run Code Online (Sandbox Code Playgroud)

当您指定自定义按钮类型时,它希望您配置所有内容...选择状态等,此处未显示,以使答案集中在手头的问题上.


Luc*_*cas 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];
}
Run Code Online (Sandbox Code Playgroud)