Reputation: 11193
I'm trying to set a BackButton as a image instead of the standard text and backArrow, but whatever i do it wont work. i've tried hiding the BackButton and setting leftBarbutton, but then the backButton is not hidng and i've tried setting backBarButtonItem as below, but still only appearing the standard backButton. What am i doing wrong?
var backButton:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "left"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("goBack:"))
self.navigationController?.navigationItem.backBarButtonItem = backButton
tried now
var backButton:UIBarButtonItem = UIBarButtonItem(image: UIImage(named: "left"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("goBack:"))
self.navigationController?.navigationItem.leftBarButtonItem = backButton
Upvotes: 0
Views: 172
Reputation: 136
Take a UIButton and set its Image, frame and add target.
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backButtonImage = [UIImage imageNamed:@"MyImage"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
[backButton setImage:backButtonImage forState:UIControlStateHighlighted];
backButton.frame = CGRectMake(0, 0, 40, 40);
[backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
Using this backButton define a UIBarButtonItem and assign that to self.navigationItem.leftBarButtonItem
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = barButtonItem;
Upvotes: 1
Reputation: 1450
You should'nt use self.navigationController?.navigationItem
but self.navigationItem
directly!
But anyway, for more background:
When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item.
(From Apples documentation of backBarButtonItem
)
That means, the backBarButton
item property can only change the back button appearance of the next viewController you push onto your UINavigationController
's stack.
To achieve the desired effect, you can
leftBarButtonItem
(this should automatically hide the back button) and call self.navigationController.popViewControllerAnimated(true)
manually when it's clickedor
UINavigationBar.appearance()
API.Upvotes: 0