问题
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:
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)
}
回答1:
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 :)
回答2:
This seems to be something like what you're describing:
Pretty simple if you use an image instead of a title for your bar button item.
来源:https://stackoverflow.com/questions/59256818/change-colour-of-back-bar-button-item-only-in-swift