UITabBarItem Image wrong size - swift

I'm using a storyboard with a TabBarViewController. The UIViewController of the tabs are embedded in UINavigationViewController. I'm using .png files and added through the attributes inspector to the view. At the size inspector the Image Inset is set "18" for top, bottom, left & right.
If the apps starts the icons are a lot too small. After tapping a tab it changes the size to the desired size.
How can I get the correct size of the icons without tapping it?
Thanks!

You have to provide your images in 2 resolutions.
If you use xcassets, just drag&drop your images in 1x and in 2x resolution on the placeholders and give it a name.
If you do not use xcassets, provide the 2 files with names:
image.png (normal resolution) and
image#2x.png (for retina)
Within your code just use image.png as filename, XCode automatically picks the correct file.

Related

Managing iPhone 5 and iPhone 6 image sizes

I am new to programming and Xcode and struggling with setting image size for iPhone 5 and iPhone 6. Apparently both iPhone 5 and 6 use the #2x image while the 6+ use the #3x image. So i can design the 6+ perfectly, however since both iPhone 5 and 6 use the same image and they have different screen size/ resolution, mu screen design is impacted.
If I design the images as per iPhone 6 size, the images appear too big on iPhone 5 and if I design them as iPhone 5 then they appear to be too small on iPhone 6. This should be simple, what am I missing?
Please help!
Instead of directly using the images you should create an entry for them in the Images.xcassets. Select it in your Project Navigator.
Then drag and drop the #3x or #2x version of your image from the images folder into the list of images in the Images.xcassets window. This will automatically generate a new entry. Then drag the other (#2x or #3x) image into its proper position. It will look like this in the end.
Once that is done you can use it in the UIImageView, or Image for a button or whatever in your Storyboard as just the name without the #2x or #3x.
Now to finally answer your question. You should handle all of the image sizes with constraints in the storyboard. So for instance if you want the same margins on either side of your image you will create constraints between the size of the UIImageView that contains your image and the edge of the Superview. This way the image will be resized for each new screen.
Edited to add Be careful of the Constrain to margins check box as you want to be consistent with using it or not otherwise you will get behavior you didn't plan for. I always uncheck it and have my constraints go to the superview edge, but it doesn't matter as long as you are consistent across your App you won't get confused.

UiTabbar Icon doesn't display correctly

I am trying to upgrade my app to display icons that look great on the retina screen of the iPhone 5. AS of right now I am not setting my icons using code, I'm simply selecting the image within xcode and it loads it on its own ( I don't know how to code in the icon so that's why I am doing it this way).
Anyways, when I set the icon I have to use a 24x24 icon or else it wont fit.
When I use my high-res icons 64x64 they do not fit correctly. My question is, how do I go about making it so my icons will be scaled to fit but still retain the high-res quality?
1) If your non-retina icon is 24x24, then your retina icon should be 48x48
2) You should set your icon to be the non-retina version in interface builder. The retina version will be swapped out automatically if its on a retina device, as long as you follow the standard naming scheme:
name.png (non-retina image)
name#2x.png (retina image)
Note also: if you do set an image in code, you can simply use [UIImage imageNamed:#"whatever"] and the retina image will be swapped out automatically for you too (you don't have to do if-else or anything like that).
just try to set this code in your .m file of your particular tabBar's parent view.. in viewDidLoad: method..
self.tabBarItem.image = [UIImage imageNamed:#"home#2x.png"];/// set your image name instead of home

xcode: storyboard Auto Pixel to Point Conversion?

When applying Retina 4 for the storyboard, it would give a resolution of 320points-by-480points which is 640-by-960 in pixels.
However, when adding an image for an UIView or an UIButton by dragging it from the Media library into the VC, [not programmatically]
let's say, the image has a resolution of 50-by-50 in pixels,
instead of appearing 25points-by-25points in the storyboard,
it would appear 50points-by-50points.
Is it possible to set the the conversion from pixel to point to be triggered automatically instead of having to compress the image to half later on?
Yes - the image filename must have #2x at the end, so interface builder knows it is a retina asset. You should also include a non- retina version with the plain filename.

Table View Cell Image Retina Issue

I have a table view that displays custom cells with an image of the item. I allow the user to choose a photo from there library, which will be displayed on the cell. The image is saved in the documents directory.
I am confused on how to handle the retina vs non-retina displays.
Let's say my image is 70 x 70 pixels, should I also keep a 140 x 140 pixel version for the retina display. What's an appropriate way to handle this situation?
Yes, you should have a 70x70 px file and a 140x140 px one also. If you name your normal image "CellImage.png", you should name the retina one "CellImage#2x.png".
If you are allowing the user to choose the image from their library I am assuming that you are resizing the image before placing it in the Documents directory.
If you are sure that the images will not be shared between different devices (as in iCloud) then you can simply save the image in the correct scale and forget about it. i.e. scale the image appropriately for the current device.
You could just save the image at 140x140 and have the presentation properly scaled by the size constraints of theUIImageView.
I don't know if imageWithContentsOfFile works the same way as imageNamed method or not but when calling the imageNamed method you don't specify the file's extension but rather just the bare filename and the method attempts to select the correct file without you having to specify the decorations i.e.
Image.png
Image#2x.png
Image~iphone.png
Image#2x~iphone.png

iPhone 4 Tab Bar Icons

I'm having some issues getting high resolution tab bar icons displaying correctly for iPhone 4. Basically I'm using the Apple guidelines of 96 x 64 for high resolution icons, but when I do this, my icon looks like it is being scaled many times in the tab bar icon area and only part of the icon appears. If I set the icon size to 30 x 30, the icon displays correctly in terms of proportions but is not sharp because of the higher resolution of the iPhone 4 display.
I've tried other sizes like 64x64 and 60x60 but I get similar problems.
Any ideas?
you have to add #2x to youre iPhone 4 image..
So if you have a 30x30 image named image.png, make a 60x60 version and name it image#2x.png..
You have to name your icon file with #2x!
But in the attributes inspector you have to fulfill the field "Image" taking off the #2x on the file name:
example:
Original file name:"icone#2x.png"
Image field: "icone.png"