Setting a Custom Image for a Back Button on UINavigationBar

前端 未结 8 2156
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 10:55

I am trying to set a custom image for the back button that automatically gets place onto a navigation bar when a new view is pushed onto the stack.

I have tried add

相关标签:
8条回答
  • 2020-12-14 11:06

    This is the code I'm using and works perfectly in my own iOS 5 app. This code is from application:didFinishLaunchingWithOptions: in the app delegate:

    UIImage * backButtonImage = [UIImage imageNamed: @"back-button-image"];
    backButtonImage = [backButtonImage stretchableImageWithLeftCapWidth: 15.0 topCapHeight: 30.0];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage: backButtonImage forState: UIControlStateNormal barMetrics: UIBarMetricsDefault];
    

    You'll may need to use a stretchable image, where the left facing "point" is the left cap


    or in Swift

    let backButtonImage = UIImage(named: "back-button-image")
    backButtonImage = backButtonImage?.stretchableImageWithLeftCapWidth(15, topCapHeight: 30)
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)
    
    0 讨论(0)
  • 2020-12-14 11:11

    Since iOS 7.0 there is a new method in API backIndicatorImage. You can use it instead of setBackButtonBackgroundImage if you don't want the image to be stretched to fit the text (for example if you want a fixed size custom back arrow). Here's an example in swift:

    let image = UIImage(named: "back_button")
        
    UINavigationBar.appearance().backIndicatorImage = image
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
    

    You can hide the text from the button using this trick:

    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -66), for: .default)
    

    for Swift 5

    var backButtonImage = UIImage(named: "back_button")

    backButtonImage = backButtonImage?.stretchableImage(withLeftCapWidth: 15, topCapHeight: 30) UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, for: .normal, barMetrics: .default)

    0 讨论(0)
  • 2020-12-14 11:14

    Try the code below, it works for me:

    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
    
    0 讨论(0)
  • 2020-12-14 11:16

    This worked for me:

    let button1 = UIBarButtonItem(
        image: #imageLiteral(resourceName: "back_arrow"), 
        style: .plain, 
        target: self, 
        action: #selector(self.backBtnTapped(_:))
    )
    self.navigationItem.leftBarButtonItem  = button1
    
    0 讨论(0)
  • 2020-12-14 11:18

    Write this method in common class and call it from the controller in which you want to draw.

    (UIButton *)drawNavigationBarBackButton:(UIViewController*)viewController 
    {
    
        UIImage *navBackImage = nil;
    
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
         navBackImage = [UIImage imageNamed:@"Back_Button"];
    
        [backButton setImage:navBackImage forState:UIControlStateNormal];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        viewController.navigationItem.leftBarButtonItem = leftButton;
    
        return backButton;
    }
    
    
    
    call from controller class and setting action
    
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
     UIButton *backButton = [CommonClass drawNavigationBarBackButton:self];
        [backButton addTarget:self action:@selector(onTapBackButton) forControlEvents:UIControlEventTouchUpInside];
    }
    
    - (void)onTapBackButton 
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    0 讨论(0)
  • 2020-12-14 11:22

    This code worked for Swift 5 and XCode 10 :

    Add this code to AppDelegate's func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool to Apply to all of your view controllers:

    //Custom back button
        let barButtonItemAppearance = UIBarButtonItem.appearance()
        barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
        barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)
        let backImg: UIImage = UIImage(named: "back")!
        barButtonItemAppearance.setBackButtonBackgroundImage(backImg, for: .normal, barMetrics: .default)
    
        let image = UIImage()
    
        UINavigationBar.appearance().backIndicatorImage = image
        UINavigationBar.appearance().backIndicatorTransitionMaskImage = image
    
    0 讨论(0)
提交回复
热议问题