开发者

How to set max width of a UIBarButtonItem

开发者 https://www.devze.com 2023-03-21 03:58 出处:网络
I have a UIBarButtonItem in a UIToolbar that is updated with changes to a title represented by a text field.The text开发者_C百科 field should not have a short maximum length.When the title is quite lo

I have a UIBarButtonItem in a UIToolbar that is updated with changes to a title represented by a text field. The text开发者_C百科 field should not have a short maximum length. When the title is quite long, the item occludes the button items to its right. How can I make it automatically truncate to a certain width?

How to set max width of a UIBarButtonItem

How to set max width of a UIBarButtonItem


Use a custom view with maximum 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;


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

How to set max width of a UIBarButtonItem

0

精彩评论

暂无评论...
验证码 换一张
取 消