I am aware this question must have been asked before, but after 2 hours of searching and failure to find a working answer, I decided to ask again.
As you all know, each iOS model has a different screen size. However when I insert my logo (or any image) into my app, it maintains a specific size for whatever iOS model I preview it in, therefore it is too small on the 6+, yet doesn't fit on the screen on the 4.
Using code or not, how can I make my images be the same scale on each iOS model?
To achieve what you are asking for you can just use Constraints throughout your project. The easiest way would probably be to just make an aspect ratio constraint on your image view. However that will only keep the ratio of width to height the same. In order to make it fit onto all the screens, you should simply add some constraint to your image view, as for example trailing space to container margin. You can set those through here:
If you set for example 0,0,0 then this will keep your logo in the upper part of yur screen and always fitting into the screen. You may also want to consider adding a height constraint, so your logo doesn't get compressed.
Hope that helps,
Julian
Within your image casset you need to have an #3x and an #2x, then it will choose the right image size for the right device.
Related
I'm having issues with constraints in XCode 12: Even if I describe accurately all measures involved, there is no way apparently to resize a 850x600 image to make it fit proportionally in the screen of an apple device. As you can see in the image, the constraints are defined to make the original image fit WITH BORDERS on the Iphone's screen, but nevertheless is turns out to be a disaster afterwards...
As you can see in the screen capture, the image overflows the screen when the model device is changed. So...we have a problem. And the answer or clue that I'm seeking night and day is...
How to make this image to become RESPONSIVE to screen size, and keep it's proportions. No way how the hell something so basic is so difficult in XCode...
must edit... look how the alignment constrainst are shown in grey...no idea why...this avoids me to try the solution provided by the good fellow above...
It seems you've defined constraints to center the imageView and having it a size of 850x600 pixels. This will keep the image of that exact same size, regardless the device it is shown on.
If you want the image to shrink, keeping the aspect ration you need to define constraint for the top, bottom, leading and trailing edge.
And you need to set the imageView's contentMode to "Aspect Fit", which is the default, afaik.
This is an image shown on an iPhone 11:
And this the same image shown on an iPhone 4s:
Edit:
One way to add those constraints is to select (highlight) the imageView and click on the button with the little square in the middle surrounded by those "T"s. :-)
everyone. For the App on which I am working I want to let users be able to select own image and set it as the background of App. The Problem I face is how to let users crop the image fitting the size of device's screen if the image is larger. Another question is if users want to select a pretty small image and set it as the background, should I just say no to it?
We don't know how you've set up your app so we can only speculate. BUT you could create a UIImageView that is behind everything else. When the user selects a picture just set that picture to that image views background....
As for the cropping...if you use constraints and tell that image view to be 0 from top, 0 from left, 0 from right, and 0 from bottom it will automatically resize based on size of device. Also you would need to set that image views scale property so the image doesn't distort...
Finally, usually when you have images on the device you create a 1x, 2x, and 3x size and Xcode will automatically use the appropriate size ...but again we have no idea how your app works...so if the user is trying to use an image that is 15 pixels by 15 pixels and have it fill the whole screen...it's going to look pixelated
You might want to write a function that gets the image size and if it fails to meet specifications you set...tell the user somehow (UIAlert...)
All of that being said...
Folk here on Stack Overflow generally don't take time to help on questions that have no code example shown...generally speaking you attempt to figure it out first and then post...so show code that you've tried, or take a screen shot of how your View Controller looks like...
On some view controllers I am using images as background for the whole screen. I was wondering what is the best practice for this, since I want to support all currently available iPhones (5-7). Normally for images I would get #2x and #3x image, but here the case is a little bit different?
Do I add 4 photos to app and then ask system on which device i am running and than load appropriate photo? This seems kinda clunky, an not really elegant.
You don't really have that many options.
You could bundle different images for the different device aspect ratios, check the device aspect ratio at runtime, and load the correct image. This implementation is flawed, as it doesn't account for future device aspect ratios. You could have a webservice which returns a correct image for a given aspect ratio, to take that work off of the client.
You could also create a single image that looks good on all device sizes, and load the image into a UIImageView and set its contentMode UIViewContentMode.scaleAspectFill to fill the entire image into the view's frame, cropping some portion of the top, bottom, or sides out.
I've solved this in my projects by using the 'xcassets'. The system basically takes care of the 'which device size' question for you. Also, play around with the differing contentMode properties (i.e.'aspectFit', 'aspectFill', 'scaleToFill').
Another thing I would watch out for is the value for 'autoResizingMask' property. In most cases if my sizing isn't doing what I think it should, I check if I gave autoResizingMask a value of 'No'.
I hope this helps!
i wonder what is the best practice for ui design for iOS regarding ui element sizing, especially buttons. In my example all buttons are based on images, no text.
I see two approaches
Absolute Sizes
PRO:
image based buttons do not need rescaling, avoids blurriness
simple setup in interface designer
CON:
buttons look smaller on bigger displays (ipad2 vs. iphone 4) relative to other ui elements
Relative Sizes
PRO:
buttons look better in whole ui appearance across all display resolutions and densities
CON:
buttons may look blurry
interface builder constraints will get a bit more complex
Did i forgot something? Or did i get it wrong?
For now i was designing the ui completely relative to the screen. So lets say a button had the width of 10% of the screen width and an aspect ratio of 1:1. When the ui was completely relative, everything seemed consistent across all devices. But my questions started in the scenario of #2x density buttons. The iphone 4 has a width (portrait mode) of 640 while the ipad2 has an width of 768. Should i create my #2x, 10% button image with a width of 77px or 68px? I would say 77 because downscaling is better than upscaling.
Well, this leads me to my question:
What is the best practice to design the UI? How should we handle the image design for buttons?
Usually the problem is the text got stretched.
You can set the strech area by following this:
https://developer.apple.com/library/ios/recipes/xcode_help-image_catalog-1.0/chapters/SlicinganImage.html
If you use gradient and it became blur when stretched, you need to draw the gradient using Core Graphics.
I'd say there's no ultimate approach for laying out image buttons in iOS and it always depends on what you're trying to achieve.
In your case I would recommend the following approach:
Use layout constraints to position your buttons properly. Use only
spacing constraints, no width or height constraints!
Set the images for your buttons (either in Interface Builder or in code). Make sure to provide each image asset in all three resolutions #1x, #2x, #3x.
If you have multiple buttons in a row give each of them a different horizontal compression resistance priority. This ensures that in case your buttons don't all fit on screen one (or more of them) will be scaled down to fit.
When assigning an image to a UIButton, that image will determine the button's intrinsicContentSize. Thus, the button will automatically take the size of the image (if no other constraints with a higher priority are present) and it will only scale down if absolutely necessary (see number 3. above).
This approach is only possible if you still leave flexible spaces next to the buttons. If you intend to split the screen equally into three columns for example and each button should take the whole width of a column you have no other choice but do let the system scale down the images for devices with a smaller screen size. It always depends on whether you can allow the buttons to size themselves or if you need to force a width (or height) on the buttons from their superview.
I need to update an app which was written for the iPhone 4 screen size to work on the iPhone 5. I have read on SO about AutoLayout and so on, but the problem is that each screen in this app is made up of a background image, which then has touchable areas and such drawn on in code. These are positioned absolutely. This isn't the way I would have designed the app, but it's the task I am faced with.
I have two problems to solve:
1) How do I load the correct background image. Do I need to create a separate image size for each and them in code query the device size each time an image is loaded? To give some context, there are well over 100 images.
2) How do I maintain the touchable areas. Is it best to just add the required number of pixels to the bottom of the app? Would this then work, or will I need to query device size and change coords accordingly every time I draw something?
Thank you,
Sam
Autolayout will work in the device is running iOS 6 or higer.
1) Yes you will need a image larger image for all the images in your app. You could overload some methods in a base class to make this more easy to load the correct image.
An other option is to stretch the image, but this is up to you.
2) I would adjust my coordinates to the size of the device.
If Apple would add an other screen size you will run in the same problem again. You should try and create an interface that can grow with the screen.