How to "hide" files from interface builder? - iphone

My project uses a huuuge number of png file, all added as resources to the XCode Project.
Now, the problem is, whenever I try to use Interface Builder, it parses all these files, and gets terribly slow.
Is there a way to differentiate between resource files that will be used/read by code, and the one that will be used in IB?

I'm not positive, but I think IB only recognizes graphics in the root of the bundle. I think if you put the graphics in a folder and only add the folder (select the "Create Folder References" radio button) to Xcode (so it shows up blue instead of yellow), IB will ignore them. Of course you will also have to specify the new path in the code to use them.

Related

Classes in Swift files inside folder references not seen by Xcode 10's compiler

We have a project with nearly 1K Swift files. It just works well if everything is inside groups, but when trying to add some folder references to directories that contain Swift files, the compiler just can't see any thing defined inside these files (classes, for example). It works well for bundle and data files, but not for source code.
I also tried changing existing groups to folders using different methods (dragging, using the menu and manually browsing, etc.) and XCode stops seeing the entities declared inside these Swift folders' files. When I switch the same folder back to a group (delete -> remove reference -> add files to project...), it works again.
Please note that I opened the File Inspector, and already ensured that the target for the reference folder was checked. Also tried to uncheck it and checked again, just in case there is just-another-XCode-bug when adding files. Nothing worked.
Another experiment that I've done was consisted of moving a folder to the root directory, so the folder was not inside a group. Didn't work either.
After each try, I always cleaned the project's build folder, just in case.
Please note that the following questions did not provide anything useful tip for fixing this:
Xcode added folder in blue and related there files are not compiling
Moving Files into a Real Folder in Xcode
And of course, this another one does not make sense to me since I want folders, not groups, because I find them much easier to use and specially, to maintain, given the large amount of Swift files of this particular project: Adding Folder to Xcode Project is not Properly added
It works well for bundle and data files, but not for source code.
Yup, well, that's because those are completely different kinds of thing, and need to be treated in completely different ways.
You can't put code inside a folder reference (blue). That would simply mean "copy this code file into the body of the app", which would be silly. That's the kind of thing you would do with bundle and data files.
If the problem is to organize your code file references within the project window, you can use:
a group (yellow with a red mark in the lower corner)
or, a folder-linked group (yellow plain and simple)
In the latter case, the code file itself will also be placed in the corresponding folder on disk inside the project window, so this is also a mode of organization on disk.
NOTE But note that you must always let Xcode itself organize the contents of the project folder! You must work entirely within the Project inspector in the project window. Stay out of the Finder. Otherwise, you'll break your project.

iOS project directory organisation

Just a quick question/opinion about how you arrange Xcode 4 directory for each of your iOS app you create.
Personally, I still add a directory called /Resources which contains sub-directories like /Fonts, /Images, /Icons, etc. I use the option "Copy items to destination group's folder (if needed)". If I need to add a new ressource, I simply add it to the right directory in finder.app in my project directory (i.e if it's an image, I add it to MyProject/Resources/Images/Foo.png) and then I simply go on my group "Images" in Xcode and add the file from there.
I once tried to use "Folder references" option to keep Xcode and Finder in sync, but IB was not able to get the images...
Also, is your Icon.png and Default.png at the root of your project? (i.e with the .xcodeproj file?) When you use Xcode 4 (in project info > summary) they add them there... Wondered if you keep them there, Cause personally I always prefer having all my images in a sample directory!
And what about photoshop files or any other files relative to your project, do you put them in the root directory too (again with the .xcodeproj file) ?
I suggest keeping all your .m and .h and .xib files in a single folder because it simplifies localizing strings when you run ibtool to extract all the NSLocalizedString references, and it also makes it easier to move files around int the virtual folder structure within Xcode when you refactor your project.
Keep third party libraries or frameworks in a separate folder to avoid mixing them up with your own code.
Within Xcode itself, I organise these class files into separate virtual folders called Views, Controllers, Model and Nibs (and Libraries for 3rd party stuff). If it's a large project I break it into folders for each component and then break those into views, controllers, etc, but again that's only virtual folders, not physical ones.
Keeping resources in a separate physical folder is a good idea just to make it easier to browse them on disk. It may make sense to split these into images, sounds, xml, etc. Within Xcode you may want to break these down into virtual subfolders by component or category (interface, content, etc) but I wouldn't suggest doing that on disk as it makes it harder to refactor your folder structure as your project grows.
It's a really good idea to keep all your images in the same folder because it avoids you accidentally giving two images the same name. Xcode won't warn you if you import two images with the same name from different places, but when it builds the app you'll end up with just one or the other, chosen at random.
You don't have to keep Default.png and Icon.png in the root (or even call them Default.png and Icon.png) but sometimes Xcode gets confused if standard files aren't where it expects.
And yeah, whatever you do, don't try to use the blue folder references when you import images otherwise you can't access them from within your code with imageNamed: or within Interface Builder. Use the yellow virtual folder references instead. Basically blue folders get copied into the app bundle as actual folders when the app is built, whereas the yellow folders are ignored and their contents go directly into the root of the app bundle. To access content in blue folders, you'll need to include the blue folder names in the path when you load them, e.g.
NSString *path = [[NSBundle mainBundle] pathForResource:#"image" ofType:#"png" inDirectory:#"nameOfBlueFolderInXcode"];
UIImage *image = [UIImage imageWithContentsOfFile:path];
I've been thinking about this a lot lately; here's the structure I decided to go with:
ProjectName
ProjectName/Assets
ProjectName/Assets/Images
ProjectName/Frameworks
ProjectName/Logic
ProjectName/Logic/Models
ProjectName/Presentation
ProjectName/Presentation/ViewControllers
ProjectName/Presentation/Views
I think this gives a good structure for growth and provides a reasonable home for most anything. Of course, add additional subfolders (e.g. Assets/Fonts) as needed. For my full rationale for the structure check out this blog post.
I do keep things like Icon.png and Default.png at the root of the project as Xcode seems finicky about it, but otherwise things are neatly organized. I keep my corresponding .m, .h, and .xib files together, but I split up the view controllers logically into subfolders by feature (e.g. Initial, Settings).

What's the best way to copy xib file(s)?

I have a ViewController.xib that I would like to duplicate so I can make some settle changes to the duplicate copies. Everything is laid out how I like it in interfaceBuilder and coded correctly in the original ViewController(.M & h.). Can this be done and what is the best way to do this?
Sadly, there isn't a way to directly duplicate a file from within Xcode. I've submitted a bug to Apple on this and you should too. It's not too difficult to do manually, though:
In Xcode, right-click on the xib and choose "Show in Finder"
Once Finder comes up with the file selected, right-click on the file and choose "Duplicate"
Rename the file as you need
Drag these new files back into your Xcode project.
Using FINDER, duplicate all three files and rename them.
Drag them back into the project in Xcode.
Rename the Class in the .h & .m files & start making your subtle changes.
However, if your changes are all just to do with positioning you could just use the one file and change the frames of the things you're moving about
OR
Just duplicate the XIB file, rename it appropriately, add to the project & make the subtle changes. Then you can load the ViewController with whichever XIB suits your needs...

Could not load the image referenced from a nib in the bundle running on device

SplitView[1290:307] Could not load the "canary04.png" image referenced from a nib in the bundle with identifier "xx.co.mycompany.ipad.SplitView"
I only get the error when running on the device. There are no problems running in simulator.
I am using the image directly in interface builder with an UIImageView.
EDIT: I have added a different new image to a different nib and that works no problems.
Anyone have any ideas?
It might not be added to your Target and in XCode 4 you can do it like this:
In your Project View, click your project name
Select your target and select "Build Phases" tab
Drag your image file into your "Copy Bundle Resources" section
That fixed it for me.
The filesystem on iOS devices is case sensitive, so it's quite possibly a case issue. (Mac OS X is generally case in-sensitive, hence you've not seen this issue in the simulator.)
As such, check that you're using the same case in the code as the filename itself.
Do you have the image in a referenced folder in XCode (blue folder)? If so you have to include the folder name in the image path, something like "Images/canary04.png"
The easy way
1 Simply do a grep in command shell to find the string of the referenced image:
grep -R 'canary04.png' YourMainDirectoryFolder/*
2 It will give you (for example) an output like this:
YourProject/en.lproj/YourViewController.xib:
<textField borderStyle="roundedRect" background="canary04.png" id="19">
3 You now know which nib file and which element it is. Just go in the file and remove the reference.
Delete the blue folder where the image is.
Import again and select "Copy items if needed".
Select "Create Groups" NOT folders
Save image as PNG-24. It works for me
You can do this to check it from your storyboard, or nib. Open it as Source Code:
Then "Find" the image name in it, see if it exists but already invalid in your project, probably you have deleted or changed its name, and now it doesn't seem valid.
Have you included the file in the target? Right-click on your Target in Xcode, select Get Info and choose the "Targets" group along the top. Make sure your intended target(s) are checked.
Good Luck.
EDIT:
The text above should have read Right-click the file (canary04.png) in Xcocde and select Get Info...
Sorry for the typo.
I had this problem when I upgraded to Xcode 4.4.1. I found that although I had deleted the images (and corresponding buttons, etc.) in the storyboards, there was still mention of them in my storyboard files (in a tabBarItems field). I manually commented them out (), Product -> Clean, and I didn't get the errors any more.
I knew there must be bogus references in the storyboard as I had previously deleted my TabBar and the current App worked without problems, just with annoying messages in the console.
I had the same issue. try to
clean
build and run
it worked with me
In my case, it was because Photoshop wasn't saving my files correctly. In the file save dialog I was clicking on myfile.png in an attempt to overwrite that file, and it was saving the file with a png extension, but in photoshop format.
I had to make sure to actually change the file format to PNG before clicking on the file I wanted to overwrite, thus saving in actual png file, thus being readable by iOS. Interestingly, interface builder showed my psd-as-png files just fine.
More generically, make sure your file is, in fact, in the format you think it is and that iOS is therefore expecting.
Solution 1 :
In my case I had copied folder containing images and even checked "Destination : copy items if needed". However it has not been added to "Copy Bundle Resources" of "Build Phases".
You can add images as follows :
Select Targets
Select "Build Phases" tab
Select "Copy Bundle Resources"
Click on "+" (Plus) button just beneath it
Locate images and add it
Solution 2 :
Delete images by selecting "Move to thrash". Again add images in your project with "Destination : copy items if needed" checked.
Note : Make sure that you add images in Assets.xcassets folder and not outside it.
I was getting this error too. I will describe my scenario:
I have a subdirectory "img" which has some png's in it.
One of these png is "ic_timeprogress_000.png"
In the storyboard (visual designer) I had an imageView and the image was set to "ic_timeprogress_000.png" (not programatically). The designer showed the imageView correctly:
Although when running the app, the error was thrown.
So in the storyboard I changed (I insist: not programatically) from "ic_timeprogress_000.png" to "img/ic_timeprogress_000.png", which lead to not showing correctly the image in the designer but showing correctly when running (no error)
Although I alter imageView.image programatically, so this "initial image" isn't ever used.
Check to make sure that canary04.png exists in your project, and that it was not deleted.
My problem was that I saved the image as .jpg and changed the name to .png (was still in jpg format). It worked with simulator
Check your image name. If it contains some spaces then image will show while running in simulator but not in the device. While running it checks Case sensitive name etc.
I had a different problem in iOS 8. In my case the problem was that I selected Device Specific for the image set, and then selected iPad. Changing this back to Universal fixed the problem.
This setting can be found in the images.xcassets file, in the third column show the attributes inspector for the image set.
In this case the images wont be displayed in the storyboard or NIB file even if you have added them to images.xcassets. They also wont show in the simulator or device.
I've encountered this issue when I was trying to set image of an imageView on Interface Builder (.xib) file.
(I was using Image Assets)
I've had 2 images with theImage.jpeg and theImage#2x.jpg
Interface Builder could be able to display true images at compile time with these jpg / jpeg formats. But I've got this error in the runtime:
Could not load the "theImage" image referenced from a nib in the bundle with identifier "org.etc.etc"
Then I changed my image files formats to .png and it worked!
Try this solution too if you have different formats for images you try to set.
I also met the problem that jpeg files cannot be loaded successfully, however I solved it without changing them to png format: for example, if you have an image named "bg.jpg", don't use "bg" in storyboard (this name is listed in the dropdown selection list), instead you should explicitly write "bg.jpg" in the name field.
For programming based image view, I guess using .jpg extension like [UIImage imageNamed:#"xx.jpg"] should also work (I did't test though).
Try cleaning the project and make sure the info.plist matches the xx.co.mycompany.ipad.SplitView
Check the image name. Then check to see if you can open the image itself in paint or photoshop. If the image was created in photoshop and there were layers sometimes there is some conflict with the .png
I had the same issue, if your images are inside sub-folders, you need to choose "Create groups for subfolders" Option when you copy the resources.
I have this problem with Xcode 5, and it seems "Combine high resolution artwork" is the culprit. I opened the app bundle, and all the images were *.tiff, which caused the image to "not be found" :S
FIX:
Open project settings, and search in the top for "combine". Change that setting to NO.
For some reason, everyone is missing this essential fix.
In my case all images where displaying fine on devices with >iOS 7 but not with iOS 6.1.
The solution was that iOS7 is case sensitive but iOS 6.1 wasn't for me. I had images named like "myImageName" and also named "myimagename" witch leads my old iPhone 3 to be unable to display these images.
So renaming them solved it.
Tried each and every answer here and nothing, so here's another glitch, your icon name cannot have dots (.) because if it does (and Xcode allows you to do that!) while the icons show well in storyboard, they won't load either in the simulator or the device.
Maybe after you set the button's image, you changed the image's name.
2015 - Still having this problem. I was referencing an image that no longer existed in one of tabbed buttons.
Solution:
Simply inspect all your buttons, UIImage etc. and make sure that the image that you are referencing actually exists within your project.
I got this problem when I accidentally added another Image set to Asset Catalog file. I renamed it after I realised the mistake, but it doesn't seem to work.
Removing the file and adding it again solves the problem.
what worked for me is making sure the file's owner in interface builder has a "view" outlet.
From IB, select file's owner from "Placeholders", then in "Connections Inspector" make sure the view outlet in connected to the main view of current XIB.
This happened because i removed the main view at some point and forgot to reconnect my new view.
Hope it helps somebody else.
In my case, the issue was the image format. They were 'jpg' files, while Xcode was expecting 'png' files. Changing the file format fixed the issue.

iPhone Interface Builder - Moved resources to sub-directory, now IB can't see them!

I had a bunch of images in my Xcode project. They were originally added without choosing "Create Folder References for any added folders". So I removed the references and re-added as per these instructions Include a resource directory hierarchy into app bundle
Unfortunately, Interface Builder will not display any of the images inside that directory. On the project tree the directory comes up with a blue icon and all the files are there!
The file names show in the IB Inspector (i.e. under the 'Image' property), but I get missing image icons for everything located in that directory.
Any ideas how to get the images showing again?
Problem solved! Turns out that blue folder references are fine, but IB will not read them!
For anyone else who cares to know, if you have the following:
/iphone-project-dir/images/pic.png
Once you've added them in a blue folder in Xcode (as detailed in link above), you can refer to them in IB as follows:
images/pic.png
In the XIB's XML it will look something like this
<string key="NSResourceName">images/pic.png</string>
Simple! The only drawback is Interface Builder's WYSIWYG preview does not process these paths!
I found the answer here http://www.iphonedevsdk.com/forum/iphone-sdk-development/6457-xcode-folder-directories.html
If you like to have your images structured in your SCM and under XCode but really doesn't mind that all images will be placed in one directory once building the final .ipa package, here is what you need to do:
Create a directory called e.g. "Images" in your root folder
Place all your images into this folder
Add a new group from within XCode and call this "Images" too
Right-click this group and choose "Add Files to ..."
Select all your files and uncheck "Copy items into destination group's folder" because they are already in there
Make sure to select your right targets
From Interface Builder you are now able to pick your images without prefixing it with the folder name and you will see a preview of your images within Interface Builder.
Gives me both my IB preview and a neat folder structure when designing my app.
Copy Images folder to your project folder.
Add this folder to project. In "Choose options for adding these files" dialog, you must check "Create groups".
"Copy items if needed" you can leave checked.
Make sure to select your right targets
Finish