且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-10-19 13:34:59

可以.这很简单.创建一个多行按钮并使用它. 技巧"是设置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];

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

"Now Playing" is in One line in UIBarButtonItem. I have to put it in two lines, like "Now" is ay top and "Playing" is at bottom.I have written the following line of code:-

UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]

                              initWithTitle:@"Now Playing" 

                              style:UIBarButtonItemStyleBordered 

                              target:self 

                              action:@selector(flipView)];


    self.navigationItem.rightBarButtonItem = flipButton;  

So i want to pu line break in between "Now Playing". So please help me out.

Yes you can. It is fairly simple to do. Create a multiline button and use that. The "trick" is to set the titleLabel numberOfLines property so that it likes multilines.

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

When you specify a button type of custom it expects you to configure everything... selected state, etc. which is not shown here to keep the answer focused to the problem at hand.