swiftfontscolorsuinavigationbarbackbarbuttonitem

Change colour of back bar button item only in swift


I am trying to change the colour of the back bar button item in swift navigation bar.

Ultimately I aim to make something line this for the navbar: enter image description here

This is my current code which gives me three back arrows as the back button item but how do I change the colour so it is three different colours in one bit of text? (Green, Yellow, Red).

    func setCustomBackImage() {
        navigationItem.backBarButtonItem = UIBarButtonItem(title: "<<<", style: .plain, target: nil, action: nil)
    }

Solution

  • There is a much more easier way, since you said " I'm still fairly new to swift ", here you go !

    //add this in AppDelegate.swift
    func application(_ application: UIApplication, didFinishLaunchingWithOptions 
    launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    let backArrowImage = UIImage(named: "icon50")
    let renderedImage = backArrowImage?.withRenderingMode(.alwaysOriginal)
    UINavigationBar.appearance().backIndicatorImage = renderedImage
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = renderedImage
    return true
    // This will change all back-arrows to whatever image you want
    }
    
    

    Here "icon50" should that "<<<" image. Td:lr , use an image.

    Hope it helps :)