Sanjay Mishra
Sanjay Mishra

Reputation: 702

UINavigation custom back button image not working in iOS 15

I want to apply UINavigation custom background image and back button image also for iOS 14 everything is working fine but as I try to run the app on iOS 15 back button image not working instead it's showing the default back button which I want to replace with custom back button image I am using below code

   if #available(iOS 15.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = image
        appearance.setBackIndicatorImage(backButtonImage, transitionMaskImage: backButtonImage)
        if Locale.current.languageCode == "fr" {
        appearance.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
        } else {
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
        }
        navigationItem.standardAppearance = appearance
        navigationItem.scrollEdgeAppearance = appearance
    } else {
        self.navigationController?.navigationBar.setBackgroundImage(image, for: .default)
        self.navigationController?.navigationBar.backIndicatorImage = backButtonImage
        self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backButtonImage
        if Locale.current.languageCode == "fr" {
            self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 14), NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
        } else {
            self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(hexString: "#231F20")]
        }
    }

Upvotes: 0

Views: 875

Answers (1)

Pankaj Teckchandani
Pankaj Teckchandani

Reputation: 745

Sharing with you a common function I have made in a BaseViewController which is the superclass of all my View Controllers. I Simply call the addBackButton function from viewDidLoad from any of the VC I want to add the back button. It also handles the back navigation automatically. (takes care if you have presented or pushed a vc)

func addBackButton(tint : UIColor? = nil, backImage : UIImage? = Constants.Images.kImageForBackNavigation){
    
    let buttonForBack = UIButton(type: .custom)
    var tintColorForImage = UIColor.white
    if tint != nil {
        tintColorForImage = tint!
    }
    
    let image = backImage!.withRenderingMode(.alwaysTemplate)
    
    buttonForBack.setImage(image, for: .normal)
    buttonForBack.imageView?.tintColor = tintColorForImage
    buttonForBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    buttonForBack.addTarget(self, action: #selector(BaseViewController.closeViewController), for: .touchUpInside)
    let barButtonItemForClose = UIBarButtonItem(customView: buttonForBack)
    
    self.navigationItem.setLeftBarButton(barButtonItemForClose, animated: false)
}

@objc func closeViewController(){
    
    if self == self.navigationController?.children.first {
        DispatchQueue.main.async {
            
            self.navigationController?.dismiss(animated: true, completion: nil);
        }
        
    }
    else{
        DispatchQueue.main.async {
            self.navigationController?.popViewController(animated: true)
        }
        
    }
}

Upvotes: 2

Related Questions