Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop UIBarButtonItem text from truncating?

I have a UIBarButtonItem in a Navigation Bar with the text title "Save". When I transition to a fullscreen UIPopoverController and then dismiss it, the text in my UIBarButtonItem gets truncated to "S..e". For all other segues and views I have no problem when returning.

I've tried manually changing the width and setting "possibleTitles" to include long words but I can't stop the truncation.

I'm using a custom font if that makes a difference.

like image 858
David P Avatar asked Mar 11 '23 07:03

David P


2 Answers

Try to init your UIBarButtonItem with a custom view.

[[UIBarButtonItem alloc] initWithCustomView:yourView];

Just make sure your custom view has the right frame (e.g. for an UILabel ,wide enough to not truncate its content). Things should work fine.

like image 116
Gocy015 Avatar answered Mar 19 '23 17:03

Gocy015


Perhaps helpful, but a UIBarButtonItem with a custom view (ex: UILabel) that is inserted into a UIToolbar can take on the intrinsic size of its contents as long as translatesAutoresizingMaskIntoConstraints is set to false. I believe this may work for UINavigationBar too:

private let barButtonLabel: UIBarButtonItem = {
    let label = UILabel(frame: .zero)
    label.translatesAutoresizingMaskIntoConstraints = false
    return UIBarButtonItem(customView: label)
}()
like image 23
jarrodparkes Avatar answered Mar 19 '23 17:03

jarrodparkes