How to properly constrain images in interface builder - ios

I'm new to iOS development and am struggling with interface builder to layout a view that has 3 images. I want three images going across the view. So the images will need to be scaled a little differently for different devices. I am hoping to do this with constraints. I am fairly close. Here is a screenshot:
The images are actually resizing and becoming bigger on the larger device screens which is what I want and the spacing is working to stop them from overlapping each other on small devices. I just need to stop the two side images from being stretched tall. I don't want to specify a height constraint because they do need to resize for specific devices sizes. Any help to add a constraint that will make the outer images not stretch tall is much appreciated.

Set the UIImageView 's content mode to be aspect fit. It will be always stretched in it's correct aspect ratio
Instead of Scale to Fill select Aspect Fit

Related

maintaining aspect ratio on launch screen

I expect there's a simple answer here. If so, sorry and thanks.
I'm trying to make my launch screen for all the different iPad and iPhone sizes. On the launch storyboard I have two UIViews, one full size for the background (backView) and one in the shape of a square (squareView). I'm trying to keep the square from stretching (i.e. I'm trying to maintain a 1:1 aspect ratio) and make it 0.75 the width of whatever device the app runs on. I know that there will be varying amounts of space below, but how can I keep the square from distorting when I switch devices?
I've been researching the constraints and diddling with them for hours. I expect someone will tell me it's easy, just do ... what?
First, I've centered my rect horizontally and vertically
Then I've set the rect's width to x0.75 of his superview
For the final step I'm setting his ratio to 1:1
That's it! Final result:

iOS: Buttons sizes relative or absolute?

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.

Any way of resizing UIImageView / Label Text Size based on iOS Device?

I am learning to use constraints in Interface Builder and have tried messing around with aspect ratio and width/height properties of UIImageViews. I was just curious if there was anyway to autoresize, sort of like "zoom" to prevent a lot of white space in bigger iPhones? (6 & 6Plus).
Example of what I mean:
Notice the bigger devices have a lot of white space, would be nice to simply resize that UIImageView up top along with label based on device, sort of like I did with that bottom button by using aspect ratio. Any ideas?
With IOS 8 apple introduced size classes as an addition to auto layout.
https://developer.apple.com/library/ios/recipes/xcode_help-IB_adaptive_sizes/chapters/EnablingAdaptiveSizeDesign.html
With size classes you can have different user interfaces for different devices and different device orientations.
For The case you are describing (dynamically resizing of Views), you can also just go with the "old" basic auto layout functionality:
Set Constraints of UIImageView to keep a fixed distance to the top
Layout guide
Set Constraint UIImageView to keep a fixed distance to your top UITExtField
Center UIImageView in Container
Set Fixed aspect Ratio of your Image
Tadaaa, you have an dynamically resizing Image that will always keep its aspect ration.

iOS full view background image, maintain aspect ratio, control clipping direction

I'm trying to place a background image on a view, using a UIImageView or anything else that will work. I want it to fill the entire view, but maintain the aspect ratio, and allow me to control which side(s) get clipped. I'd like to use Auto Layout if possible (not code).
By setting Auto Layout constraints to keep the UIImageView the full size of the container, and setting Scale Mode to Aspect Fill, I am very close. The only problem is the image is always centered in the view, in that it is cropped on top and bottom in landscape layout and both sides in portrait. I want to be able to control that. I want to be able to "crop just the right side" (keep left edge docked to left of container), or "crop just the top". I have different images and I'll want each one to crop a different way, and possibly I'll allow users to use their own images and select from a few cropping options.
In this image, I currently get the top result, but want the option to get the bottom. The red box shows the visible area in portrait view (roughly):
My question is similar to this one: iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge
However, I do not feel it is a duplicate because my needs are different, and I'm also including an image to help explain better. Also, I will not accept an answer of "use different images for portrait vs landscape" because that doesn't really solve the problem. If you understand my question, you would see that you would actually need a different image for every different aspect ratio, pre-cropped in each alignment I want to support. Obviously that's not really a robust solution.

iOS Aspect Fill Image Resizing with Content Aligned to Left/Top Edge

I am developing a universal app for iOS-7. this particular question is specific to iPad only. I want to place an image as background of root view of a View Controller i.e. the image should fill the whole screen. The 1x image has size:768x1024. This works for Portrait orientations as non-retina resolution of iPad is also 768x1024 in portrait. In Landscape however the image does not fit. I have tried using ScaleToFit but since aspect ratio of image is not preserved I can not use ScaleToFit(their are things in image which look odd when not scaled proportionally in both axis). AspectFill resizing seems most suitable for my need, but their is a small problem. As defined in Apple Documentation "The content is resized to completely fill the bounds rectangle, while still preserving the aspect of the content. The content is centered in the axis it exceeds.". I do not want the content to be centered in axis in which it exceeds, I want it to be aligned to top/left edge.
So basically I want two things:
Aspect Fill
The content remains aligned on the Left/Top edge.
Is it possible to achieve this. Any code-snippet will be great.
Thanks
You will have to subclass the View Controller's view and manually scale and align.
Look at the code snippets in THIS answer.
Adjust imageViewXOrigin and imageViewYOrigin to align however you want.
Really the only way to get around the content fill mode is to have two different images, one for each orientation. I'd suggest changing the image in the view controllers willAnimateToOrientation: method so that you can put the image changes inside UIKit's animation block.

Resources