I found image literals to be rather distracting than useful.
Is there any way to disable this Xcode feature?
Asked
Active
Viewed 1,636 times
12

LinusGeffarth
- 27,197
- 29
- 120
- 174

Krypt
- 434
- 4
- 12
-
1Just normally initialize the image like `UIImage(named: "...")` then. – LinusGeffarth Jun 09 '18 at 15:20
-
Trouble is It is already in project I'm currently working on. It looks like rows of identical empty spaces with no context. – Krypt Jun 09 '18 at 15:24
-
I do agree that this is a design flaw. I don't mind an image being displayed there but I have problems with the code being hidden and impossible to be edited. – Sulthan Jun 09 '18 at 15:49
-
4You can comment it out which will convert the image into text, then edit it and uncomment after. I love this image (as well as color) literal feature. Easy to use and really convenient. – LinusGeffarth Jun 09 '18 at 15:51
-
1it's probably the stupidest, most totally bizarre, feature, in all of software engineering today. it's .. incredible .. that you can't just turn it off ! – Fattie Jul 09 '19 at 14:23
-
I appreciate this answer, but I would really love a solution, which turns off the "preview" kind of behavior with all other objects. Like colors, images, and the like. – MikeyE Apr 18 '20 at 21:12
-
Android Studio handles this so much better. It just shows the image previews in the gutter next to the line number. – Gavin Wright Jun 06 '22 at 06:34
1 Answers
12
A good method for this is to replace all occurrences of #imageLiteral
with UIImage(imageLiteralResourceName:)
initializers (thanks for the suggestion, @D6mi!). Here's how you can do it automatically:
Navigate to Find/Find and Replace... (or press ⌥⌘F).
Open the dropdown list on the right side and select Regular Expression.
For the search term, enter the following regex:
#imageLiteral\(resourceName: (.*)\)
For the replacement, enter this:
UIImage(imageLiteralResourceName: $1)
This regular expression captures the value of the resource name with
(.*)
and inserts it again with$1
. The backslashes are for escaping the parentheses, since they count as special characters.
Note that you don't have to use regular expression in this case (as LinusGeffarth pointed out), but it can be more useful in more complex cases than this.

Tamás Sengel
- 55,884
- 29
- 169
- 223
-
4How cool, I was going to suggest this but didn't know we could keep the image name found in the regex. Technically, though, we don't even need to use regex because we could replace `#imageLiteral(resourceName:` with `UIImage(named:` which would result in the same... – LinusGeffarth Jun 09 '18 at 15:50
-
@LinusGeffarth You're right, regex isn't necessary in this case. However, it's more customizable and if the initializer is more complex, it can be useful. – Tamás Sengel Jun 09 '18 at 15:51
-
Absolutely agree! One question for my understanding, though: how do I know what `$1` will be? – LinusGeffarth Jun 09 '18 at 15:52
-
@LinusGeffarth The first captured group is always called $1. If you have more captures, they will be called $2, $3 etc. – Tamás Sengel Jun 09 '18 at 15:54
-
Group would mean the first thing in parenthesis? In this case that would be `(.*)` right? – LinusGeffarth Jun 09 '18 at 16:01
-
-
1Well. This solusion solves the issue, But I will preffer to not pus any changes into reposetory. I'm not the only developer on the project and it can cause unnessesary merge issues. – Krypt Jun 10 '18 at 08:12
-
2Didn't know that you can do this, thanks Tamàs! I would suggest using the `UIImage` initialiser though - `init(imageLiteralResourceName:)`, as the resulting `UIImage`'s `not an optional`, while the `named` initialiser is `optional`. – D6mi Sep 19 '18 at 06:27
-
I agree with @Krypt: this answer, however useful - does not answer the question really. I also want to know if it is possible to disable previews without changing repo. – Lukasz Oct 04 '18 at 06:56