Set images for different iphones and debug - ios

I am currently developing an ios app and the scope is limited to iPhones only and portrait mode only.
In each screen there are images, buttons, labels and textFields. The question here is, how do I render appropriate image for device from the Images.xcassets?
What I am doing now, written below -
From Sketch design application, exporting my images for 1x, 2x and 3x (Screens designed by a UI designer)
Adding them to Xcode project (drag and drop from finder to xcode)
Add new image set in Images.xcassets (naming it as "MyImages") and drag drop my images for 1x, 2x, 3x.
In the storyboard, on a viewcontroller, adding an ImageView
For the image view, selecting Image as "MyImages" from Attribute Inspector.
Once the above steps are complete, when I test the app on simulators starting from iphone4s, iphone5, iphone5s, iphone6 and so on.. (all the simulators available on Xcode 7.3), I don't see the appropriate image is being rendered.
Is my approach correct?
Also, how do I debug a UI element on the screens? Like, how/where do I check for what image is rendered? Its size (W X H)?
Added Screenshot

#Lohith Korupolu:
The screen shot you provided is for universal size (iPhone/iPad). From the additional information provided in comments, this would lead to issues with AutoLayout constraints that would stop the image showing on screens of a smaller size than that shown in your StoryBoard.
E.g. I have replicated your issue on storyboard...
This results in the following in Simulator for iPhone 4s....
i.e. Text is there but NO IMAGE.
REASON: The autoLayout Constraints set for the larger "Universal" Screen on any of two opposite sides would make the image invisible/ disappear when viewed on a smaller screen.
SOLUTION:
1. Clear the Autolayout constraints for this selected Image in Storyboard. See next picture...
2. Add AutoLayout constraints as below x2 pictures. (Top constraint, Height + Width) (horizontally in container). Remembering to tick "Items of new constraints" for both.
3. Run Simulator for iPhone 4s... E.g.
** The same situation is occuring with the other iPhone size simulator Runs. **
****** All Working ******

They should all be 2x images for iPhones apart from the plus size iPhones which should be 3x. Is that not what you are seeing?
In terms of debugging / checking this - you can put place holder images in your assets with labels or different colour tints to distinguish them from each other and then replace them later with images that give the desired final appearance once you know that everything is working as expected.

Using 2x images for iPhones approach works fine, however, there is another more straight forward way without having to resort to multiple image files for 1x, 2x, 3x in Xcode, by use of good large quality PDF (vector).
1. Create a Large PDF of the image/Graphic image you want to use
2. Import it into 'Assets.xcassets' - drag and drop (Into Xcode)
3. Go to the utilities panel on the right for the 'Attributes Inspector' (when the image is selected)
See Screen shot (a)
Under 'Scale Factors' the selection from 'Multiple' to 'Single Vector'
Now when you got to 'StoryBoard' and add the image - Simply select the PDF's name.
Xcode will automatically render it to correct size etc at run time for you. All the work is done by Xcode.
Exceptions:
It does not work well with images for icons inside the TabBar or Navigation Bar Items.
Note:
Vector graphics are sharp and ideal for High Definition (HD), but although Xcode accepts the Image Asset as a vector from the PDF, it doesn't seem to keep the vector but converts it into an actual image with pixelation problems when zoomed in, from a HD perspective.

Related

My exported sketch images won't work on different screen sizes in Xcode

I designed an interface in sketch for an iOS app that I am creating and then exported the artboard as a pdf and placed it in Assets.xcassets in Xcode. I am now trying to make that image show up on the entire screen for all screen sizes. When I created the image in sketch, I used an iPhone 6 screen size (375 by 667). In Xcode now, the image takes up the whole screen on a iPhone 6 screen, but it doesn't take up the whole screen on any other device. It is too large for an iPhone 5 screen, so only part of the image is shown, and it is too small for an iPhone 6+ screen so there is white space on the screen. How do I make Xcode scale this image for all the different screen sizes? Thanks
1) First off make sure you're actually saving the asset as a vector:
It'll say Universal under and on the very right you'll notice it says Scale Factors: Single Vector
Now if You're still having issues it probably just means you haven't added constraints to the image. So it's just using the default size (the size you exported in).
If you're using storyboard or XIB make sure the image takes up the space it wants, click on it, then press on the little triangle on the bottom right of the storyboard and under Selected Views press on 'Reset to Suggested Constraints'
This should make sure the image resizes with different views. But because you might get different dimensions you'll probably want to set the UIIImageView's mode. Again on storyboard you can click on the imageView and then:
If you want the image to keep its aspect (not stretch in any direction) but want it to fill the entire space it has, put the mode on Aspect Fill

Xcode 7 auto layout always renders 4:3 ratio

After upgrading my project from Swift 1.2/Xcode 6 to Swift 2.0/Xcode 7 all of my view controllers are rendering in 4:3 ratio (iPhone <=4). On iPhone <=5 then black space pads the top and the bottom of the screens.
Nothing has changed with the storyboard, and I am not dynamically sizing the views or using any other code to manipulate the view size.
What could be causing this issue, and how to resolve? I have tried a number of hacks including requesting full screen in the info.plist but cannot get it to work. It is frustrating since everything worked perfectly before Xcode7.
Below is a screenshot from an iPhone 6 showing the black space above and below the controller.
I resolved it by choosing 'Launch Screen File' as '*.storyboard' from Project Settings -> Targets -> App Icons and Launch Images.
It happens cause you do not have Launch images properly set.
Go to image assets
Hit + => "App Icons & Launch Images" => "New iOS Launch Image"
Now you created a special image asset for launch image. Open it (it has 'LaunchImage' default name) and specify images for all kinds of screen width/height ratios.
Also you have to look through project navigator and remove all files that probably exist as launch images (they are named 'Default.PNG', ''Default2X.PNG' or something).
Suggesting a workaround:
If it is an iPhone only app you can simply disable Size Classes from Storyboard: click on view -> enter file inspector (first tab of right panel) and uncheck "Use Size Classes".

Where to put launch images?

This has been an amazingly exasperating and non-transparent process.
So far, from this question I have figured out the different dimensions of launch images I needed and that I can safely not use launch xibs / storyboards if I already have the launch images, and from various posts I have determined that the naming convention (APPARENTLY??) should be Default#2x.png for iPhone 4s, Default-568h#2x.png for iPhone 5, Default-667h#2x.png for iPhone 6, and Default-Portrait#3x.png for iPhone 6+, although I have seen conflicting information on this so I'm not even sure....do I need ~iphone appended to the names?
BUT THEN, I go to add them to xcode so I go to my images.xcassets thing and select "new launch image" and this chart thing appears that makes zero sense to me....it only has space for one landscape picture, "Retina HD 5.5", but not for the other landscapes....even though the project is supposed to be portrait-only anyway...and if this thing is keeping track of my images then what do I need the naming convention for? This is so confusing.
Okay, the easiest way to make a nice launch image is to use a xib. You won't have to worry about naming conventions and it will scale beautifully across all your devices.
Step 1:
Your project should come with a file called LaunchScreen.xib. If you don't see one like that, hit File > new File > User Interface > Launch Image.
Step 2:
Open the xib and click on edge of the xib. Then, click on the Attributes inspector. Set the size to whatever you are using in the rest of your app. We are taking advantage of the auto layout and so the image should be resized to fit other devices.
Step 3:
Drag in an image view and resize it to fill the xib. Insert a high resolution version of your splash screen.

How to prevent Xcode/Interface Builder from auto-shifting view down?

Without modifying any code in a new Xcode 6.0 Single View project targeting iPhones running iOS 7.1, the result on my testing iPod Touch 5th gen device shows a shifted version of what's in Interface Builder. It appears the shift is downward. I've already tried turning off Auto Layout and Size Classes, and resetting all UIView coordinates to (0,0), but still the battery symbol and upper bar is too far down! Please see pictures below for Interface Builder screenshot, and device screenshot. The question is how can I keep what I see in Interface Builder, and stop the iPod from shifting down the entire UIViewController? Thanks!
UPDATE: In the end, I had to go into project build settings, click on the app under Targets, and change Launch Images Sources to a new folder it titled "LaunchImage-2" and delete the Launch Screen File field, which was set to Launch Images.
If you don't add the Default-568h#2x.png
then xcode build the project of size 3.5 inch and shows black patch at the bottom.
So you have to compulsory add the image with name Default-568h#2x.png of size 640 x 1136
If you use an empty Asset Catalog for your Launch Images, the iPhone 5's screen size is supported (without having to include any image files at all).
It seems that you need to add the default or launch images into the project. Your project should be using image assets, so there you will see launch images and you need to supply images of appropriate sizes, preferably for all iPhone screen sizes.

How can I get the Xcode storyboard and the iPhone Retina (3.5-Inch) simulation to use the correct xcassets images?

Weird problem here which may be a bug in Xcode...
Using an Xcode 5.1.1 storyboard with auto layout, I am working on the design of a view controller that contains a full iPhone 4 sized UIImageView that references an xcassets image set. The xcassets image set includes 1x and 2x iPhone 3.5-inch images, and an iPhone 4 R4 image.
Problem 1: Once I set the Image View Image for the UIImageView in the utilities panel using a View Mode of Aspect Fit, the storyboard shows the smaller iPhone 3.5 image with white spaces at the top and the bottom of the view instead of the iPhone 4 image which would fill the entire storyboard representation of the view, as they are the same size since Xcode infers an iPhone 4 sized screen in the storyboard.
Problem 2: If I ignore problem 1 and run the program using the iPhone Retina (4-Inch) simulation, the applications selects the correct image size from xcassets and everything seems fine. However, if I run the simulation using the iPhone Retina (3.5-Inch) simulation, the applications also selects the correct sized image, but pushes the images down and display white space at the top of the page.
Before I start hacking around these problems, I thought I would reach out and see if anyone else has experienced either of these issues. Any help will be greatly appreciated.

Resources