Danh DC Danh DC - 1 year ago 262
iOS Question

Display "..." when backBarButtonItem have long text

i'm a newbie with iOS programming. I work with app use Navigation Bar, and set text for Back button like this:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"my text" style:UIBarButtonItemStyleBordered target:nil action:nil];

it work fine. But, the problem occur when my text is long, backBarButtonItem display with default text is Back.

So, the question is : How can i display long text in backBarButtonItem, maybe display "..." instead of full text. Or Can i use custom view replace with default Navigation Bar?

P/s : i have refer "back" text displayed in iOS7 UINavigationBar when view title is long but it not helpful for me to solve this problem.

Answer Source

Try this

self.navigationItem.hidesBackButton = YES;
UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom];
btnLeft.frame=CGRectMake(0, 0, button.widh, button.height);
[btnLeft addTarget:self action:@selector(onClickBackBarItem:) forControlEvents:UIControlEventTouchUpInside];
[btnLeft setTitle:@"YOUR TEXT"  forState:UIControlStateNormal];
btnLeft.titleLabel.font  = [UIFont fontWithName:@"font_name" size:25.0];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnLeft];

May this help you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download