backBarButtonItem gets renamed in iOS 7 when there

2019-04-06 00:55发布

One behavior I observed in iOS 7 is that the title of the backBarButtonItem of a UINavigationItem get's renamed if the title of the currently displayed view controller is too long. Too lazy to explain so here are some photos:

Screenshot with short title and desired back button

Long title renames back button to "Back"

As you can see, when the title is too long, the back button gets renamed to "Back" regardless of what it was previously. If the title is even longer, the back button doesn't show any text, just the left arrow image.

Does anyone know how to disable this behavior? I would like the back button to stay exactly what I want it to be and not get renamed. Thanks

EDIT

I created a dirty solution by manually constraining the width of the title of the view controller. I discovered that the font of the title on iPhone is System Bold 17.0, so I do a check for what the size the title will be before setting it (via the sizeWithAttributes: method of strings), and trim characters off the end of the title until the size is shorter than the length that causes the back button to get renamed.

1条回答
迷人小祖宗
2楼-- · 2019-04-06 01:41

iOS 7 will automatically replace your back button title with "Back" or even remove the title altogether in order to fit the title of current navigation item. You probably shouldn't try to do anything about it except maybe try and make your titles shorter.

if you want to make short title you can do as below

self.title = @"SOME REALLY LONG NAVIGATION BAR TITLE";  
UILabel* label=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];  
label.text=self.navigationItem.title;  
label.adjustsFontSizeToFitWidth=YES;  
self.navigationItem.titleView=label; 
查看更多
登录 后发表回答