Chenya Zhang Chenya Zhang - 2 months ago 5
Objective-C Question

How to make bar button items have 2 lines of text?

I try to follow this one, but it doesn't work.

How can we put two line in UIBarButtonItem in Navigation Bar

I don't know why the button view I created and assigned to the bar button item cannot display at all?

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.titleLabel.numberOfLines = 0;
[button1 setTitle:NSLocalizedString(@"REQUEST\nEQUIPMENT", nil) forState:UIControlStateNormal];
[button1 sizeToFit];
self.barButton = [[UIBarButtonItem alloc] initWithCustomView:button1];


Or is there any better way to have more than 1 line of text in the UIBarButtonItem?

Answer

The problem is that you are making a bar button item but you are never putting it into the interface. Setting self.barButtonItem doesn't put the bar button item into the interface; it just retains the bar button item. This has nothing at all to do with the "two-line" question. You cannot make any bar button item appear by doing what you're doing.

A bar button item appears when, say, you are a view controller in a navigation interface and you set self.navigationItem.rightBarButtonItem to your bar button item. But you are not doing that.

Comments