ASH ASH - 5 months ago 17
iOS Question

How do i remove text from back button on navbar within Xcode?

I set an arrow custom image to navigation bar by adding the following code in app delegate, it works but now im looking to remove the text completely for back button.

UIImage * backButtonImage = [UIImage imageNamed: @"BackButtonGrey.png"];
backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];

Answer

I just did it like this, worked fine for me :-)

UIImage *backButtonImage = [UIImage imageNamed:@"navigationBarBack.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

[backButton setImage:backButtonImage
            forState:UIControlStateNormal];

backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);

[backButton addTarget:self
               action:@selector(popViewController)
     forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;