1

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.

Community
  • 1
  • 1
Danh DC
  • 1,246
  • 7
  • 17
  • Possible duplicate of ["back" text displayed in iOS7 UINavigationBar when view title is long](http://stackoverflow.com/questions/20282760/back-text-displayed-in-ios7-uinavigationbar-when-view-title-is-long) – Guy Daher Feb 17 '16 at 03:52
  • @GuyDaher , sorry but it not helpful and no answer accepted to solve this problem – Danh DC Feb 17 '16 at 03:55

2 Answers2

2

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

Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
1

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];
Madan gupta
  • 668
  • 6
  • 19
  • probably you cannot display long text to backBarButtonItem, so you need to set leftBarButtonItem and perform [self.navigationController popViewControllerAnimated:YES]; on backActionEvent (i.e. UIControlEventTouchUpInside). – Madan gupta Feb 17 '16 at 07:07
  • Yup, but how can i show "<" arrow if i set **leftBarButtonItem** – Danh DC Feb 17 '16 at 07:09
  • if you want to show arrow than you need to set arrow image in UIButton object (before text). – Madan gupta Feb 17 '16 at 07:12
  • I'll try, but have small question, how can i limit length of button title, because i have use **rightBarButtonItem** – Danh DC Feb 17 '16 at 07:19
  • refer this for image + text in UIButton http://stackoverflow.com/questions/11717219/uibutton-image-text-ios and back arrow image (change image colour as per your convenient) https://cdn4.iconfinder.com/data/icons/ionicons/512/icon-ios7-arrow-back-128.png – Madan gupta Feb 17 '16 at 07:20
  • you cannot limit it by char count but you can limit it by its width size , so set width size as per your text char count and don't use sizeToFit property – Madan gupta Feb 17 '16 at 07:22
  • Sure, i think this is the best solution for now. I'll make accepted for your answer. Thanks bro – Danh DC Feb 17 '16 at 07:29