Change colour of back bar button item only in swift

余生颓废 提交于 2020-01-01 21:59:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!