CalZone
CalZone

Reputation: 1721

back button uinavigationcontroller

I'm trying to make a custom back button to pop back to parent in nav controller. I understand I should've made the back button in parent controller itself

self.navigationItem.backBarButtonItem= backBarButton

The button was just default button with custom text, but it worked. None of formatting or fonts showed up. So instead I made a custom leftBarButton in child VC:

UIButton *backBtn= [[UIButton alloc] init];
backBtn.backgroundColor = [UIColor clearColor];
backBtn.titleLabel.font = [UIFont fontWithName:@"CrimeFighter BB" size:20];
backBtn.titleLabel.textColor = [UIColor whiteColor];
backBtn.titleLabel.text = @"back";
[backBtn addTarget:self action:@selector(pop) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
self.navigationItem.leftBarButtonItem = backBarButton;

Its working fine, but the button is not visible at all!

Upvotes: 0

Views: 129

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

New attempt:

Doing an alloc & init on a UIButton isn't the right way to create a button in code.

The way to programatically create a button is:

UIButton * backBtn = [UIButton buttonWithType: UIButtonTypeRoundedRect];

Original attempt:

You need to give your custom back button a proper size. Try doing this before creating the "UIBarButtonItem".

backBtn.frame = CGRectMake(20.0f, 0.0f, 70.0f, 30.0f);

The last number is the height and the second to last number is the button width.

Upvotes: 1

Related Questions