Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add a back button to a UINavigationBar?

I am using the following code to achieve a navigation bar in my app. (my app was crashing when I used a push segue so I need a modal segue meaning the nav bar is hidden after the modal segue is called)

UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
//do something like background color, title, etc you self
[self.view addSubview:navbar];

Does any one know any methods I can use with the above code to achieve back button functionality in the nav bar??

like image 443
thisguy Avatar asked Nov 07 '25 02:11

thisguy


1 Answers

Use below code to add back button on left side of navigation bar.Add UIBarButtonItem on Navigation bar.

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] 
                           initWithTitle:@"Back"                                            
                           style:UIBarButtonItemStyleBordered 
                           target:self 
                           action:@selector(backBtnClicked:)];
self.navigationItem.leftBarButtonItem = backButton;
like image 95
poojathorat Avatar answered Nov 09 '25 16:11

poojathorat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!