How to set max width of UIBarButtonItem

I have a UIBarButtonItem in a UIToolbar that updates with a change in the name represented by the text box. The text field should not have a short maximum length. When the title is quite long, the element covers the button elements on the right. How can I make it automatically crop to a specific width? Interface builderSimulator

+6
source share
2 answers

Use a custom view with the widest possible width, textAlignment set to UITextAlignmentCenter and lineBreakMode to UILineBreakModeTailTruncation .

 UILabel* l = [[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)]autorelease]; //200 is just a number I choose. you should calculate your maximum possible value l.textAlignment = UITextAlignmentCenter; l.lineBreakMode = UILineBreakModeTailTruncation; self.navigationItem.titleView = l; 
+4
source

You can also set the width in the Storyboard Size Inspector:

enter image description here

+1
source

Source: https://habr.com/ru/post/893081/


All Articles