[SWIFT] [IOS] What to do when the image is filled with one color

There are times when you want to put the original image in the NavigationBar and make it a little richer, right? At that time, if you try to do it in the usual way, it will be displayed in blue.



I would like to keep a memorandum of what I can do to avoid this.

The usual way

First, try as usual. Is it like this when written in code?

    override func viewDidLoad() {

    private func setupNavigationBar() {
        navigationItem.title = "Main page"
        let button = UIButton(type: .system)
        button.setImage(UIImage(named: "button_refresh"), for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        let rightItem = UIBarButtonItem(customView: button)
        navigationItem.rightBarButtonItem = rightItem
    @objc private func buttonAction() {
        print("The color of the button is strange! !! !!")

Actually, it's faster to add it in Storyboard, but since this is a group that writes in code, I usually do this (Iron's will).

If you write it like this for the time being, the button icon will be filled with blue, which makes you feel sad.

Remedy 1 (when writing in code)

When writing in code, it is OK just to rewrite the place where setImage is set in the button. Specifically, it looks like this.

button.setImage(UIImage(named: "button_refresh")?.withRenderingMode(.alwaysOriginal), for: .normal)

UIImage has Rendering Mode,

automatic Default. It will select the mode when you use the image.
alwaysOriginal Always display the original image
alwaysTemplate It depends on the tint color of the button etc.

You can specify three. This time, I want to display the original image on the Navigation Bar, so I specified ʻalways Original. On the contrary, if you want to match with tintColor, you can specify ʻalwaysTemplate.

Remedy 2 (when specifying directly in Assets.xcassets)

However, for those of you who find it cumbersome to write in code, there is a direct way to do it in Assets.xcassets. First, open Assets.xcassets, select the desired image, and then

Look at the second Renser As from the top of the Image Set items on the right. If nothing is specified, it should be Default.

You already know the rest. If you change the value of Renser As to ʻOriginal Image, it will automatically become ʻalways Original without using with Rendering Mode in your code.


That's why it was the setting method for Rendering Mode of UIImage. Even though it is a function that you use unexpectedly, you may not be able to remember it by accident (4 losses), so please be careful.

Recommended Posts

[IOS] What to do when the image is filled with one color
[Rails] What to do when the view collapses when a message is displayed with the errors method
What to do if the background image is not applied after deployment
Notes on what to do when EC2 is set up with t2.micro
What to do if the app is not created with the latest Rails version installed when rails new
What to do when is invalid because it does not start with a'-'
What to do when The SSL certificate has expired
ParseException: What to do when Unparseable date is reached
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
After installing'devise''bootstrap' of gemfile with rails, what to do when url is an error
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
What to do when you launch an application with rails
[Joke] What to do when the invalid sample rate is reached when using Mixxx on Ubuntu 20.04
What to do when javax.batch.operations.JobStartException occurs
[Ubuntu 20.04] What to do if the external monitor is not recognized
When requested access to the resource is denied when pushing with Docker
What to do if validation doesn't work with the update action
What to do when the changes in the Servlet are not reflected
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
What to do when Gradle says "For more information, please recompile with the -Xlint: unchecked option"
What to do when you want to know the source position where the method is defined in binding.pry
What to do when Maven says "For more information, please recompile with the -Xlint: unchecked option"
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What to do when a javax.el.PropertyNotWritableException occurs
What to do when undefined method ʻuser_signed_in?'
What to do if the prefix c is not bound in JSP
What to do when CentOS cannot be started with VirtualBox on Catalina
What to do when "npm ERR! Code ENOSELF" is displayed after npm install
What to do if tomcat process remains when tomcat is stopped in eclipse
What to do if you get Application with name appName is already registered. When you try to start GlassFish
What to do if zip dies if there is a pom when making an executable jar with gradle
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do when you want to delete a migration file that is "NO FILE"
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
What to do if the server tomcat dies
What to do when rails db: seed does not reflect in the database
What to do if you get an "A server is already running." Error when you try to start the rails server
[Grails] Error occurred running What to do when the Grails CLI does not start
[Swift] What to do if the app icon is set but not reflected
Androd: What to do about "The Realm is already in a write transaction in"
What to do when debugging "Source not found"
What do you use when converting to String?
What to do when IllegalStateException occurs in PlayFramework
WELD-001408: What to do when Unsatisfied dependencies for type Bean with qualifiers @Default
What to do if you hit the wall of Too many symbol files when uploading with App Store Connect
What to do when ‘Could not find’ in any of the sources appears in the development environment with Docker × Rails × RSpec
What to do when booting Ubuntu on WSL2 and getting "Process terminated with code 1"
[React.useRef] What to do when the latest state cannot be referenced in the event listener
I tried to summarize the points to consider when acquiring location information with the iOS application ③
What to do when rbenv says that there is no readline or lib history
I tried to summarize the points to consider when acquiring location information with the iOS application ①
What to do when build fails by rbenv install / pyenv install with anyenv (Debian system)
A memo to do for the time being when building CentOS 6 series with VirtualBox
What to do when Address already in use is displayed after executing rails s
What to do when routing settings do not work after building Docker environment with Laravel
I tried to summarize the points to consider when acquiring location information with the iOS application ②
What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if the breakpoint is shaded and does not stop during debugging
[DDD] What is the most accessible architecture to get started with Domain Driven Design?
A warning is displayed when trying to use a huge integer with the special variables $ 1, $ 2, $ 3 ...