I need to set title for Custom UIBarButtonItem.
I've the following code:
UIButton* backToRecent = [UIButton buttonWithType:UIButtonTypeCustom];
[backToRecent setImage:[UIImage imageNamed:@"back.png"]forState:UIControlStateNormal];
[backToRecent addTarget:self action:@selector(backToRecent) forControlEvents:UIControlEventTouchUpInside];
[backToRecent setFrame:CGRectMake(0, 0, 60, 30)];
UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backToRecent];
[backButtonItem setTitle:@"Recent"];
partViewController.navigationItem.leftBarButtonItem = backButtonItem;
[backButtonItem release];
But the button appears without the "Recent" text.
Select the UIBarButtonItem in Interface Builder , open the inspector (Command + Shift + I) , and select "Custom" under the dropdown next to Identifier .
In case you want to initialize UIBarButtonItem programmatically in Swift 4+ should be done as following: editButton = UIBarButtonItem(title: "Edit", style: . plain, target: self, action: #selector(editAct)).
A specialized button for placement on a toolbar, navigation bar, or shortcuts bar.
Muhammad, I suspect you have resolved this already, but here is an easy way. Hope this helps someone who is struggling with the same issue:
UIBarButtonItem * backButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Recent" style:UIBarButtonItemStyleBordered target:self action:@selector(backToRecent)];
self.navigationItem.leftBarButtonItem = backButtonItem;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With