Danh DC
Danh DC

Reputation: 1246

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.

Upvotes: 1

Views: 398

Answers (2)

Madan gupta
Madan gupta

Reputation: 668

use sizeToFit to show full text in UIButton

    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 20) ];
    [btnBack setTitle:@"Skip" forState:UIControlStateNormal];
    btnBack.titleLabel.font = [UIFont fontWithName:@"SFUIText-Medium" size:15];
    [btnBack sizeToFit];
    [btnBack addTarget:self action:@selector(backActionEvent:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSkip];

Upvotes: 1

Kamlesh Shingarakhiya
Kamlesh Shingarakhiya

Reputation: 2777

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

Upvotes: 2

Related Questions