iOS: Buttons sizes relative or absolute? - ios

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.

Related

What is the proper way to calculate Left, right, top, bottom constraint according to different devices?

Suppose i have a View controller in which there are two labels and they have 112 vertical spacing between them for iphone 6 plus. If i want to keep this spacing for all types of devices as a aspect ratio, what will be the proper way?
Currently i am following one of two approaches.
1. Stack View approach:
In this approach i am taking
a. A stack view
b. Put that two labels into that Stack View
c. set spacing from storyboard.
d. multiply this spacing with the device aspect ratio. like this
#define SCREEN_RATIO_RESPECT_OF_IPHONE_6P (SCREEN_MAX_LENGTH/736.0)
2. Normal Constraint Approach:
Multiply vertical spacing(in my case 112) with the device aspect ratio just like approach 1.
But what is actually the better way to handle this type of problem?
For your view to remain visually consistant across devices I usually convert my design specs into relative or percentage based sizes.
So for example, if you have a button at the bottom of the screen that is supposed (as per design) to have 24px of space from the bottom and side edges of the screen. I would look at the design and work out 24px as a percentage of the width of the screen (in the design). This would mean that the spacing/padding is consistent on all devices.
In regards to fonts, I would usually set the font size as per design but where possible I would allow it to reduce the size to fit using adjustsFontSizeToFitWidth and minimumScaleFactor like in UILabels.
Hope this helps

How to properly constrain images in interface builder

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

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.

UIButton Image size

I have 2 UIButtons. I set the background images to 2 different pngs. The pngs have different width. I looks like this:
The button size is set to the images.size.width and height.
I would like these buttons to be the same size, but when I resize the buttons manually, the images gets distorted, like this:
Is it possible to resize my buttons without distorting the image? Since it's a picture, I thought stretching doesn't make sense.
Any help would be appreciated.
Thanks!
As you guessed, you are stretching the pictures size by resizing the buttons. If I was you, I would look into manually resizing the images so that they are the same dimensions as your buttons.
Without distorting or ruining the integrity of the image the only real solution would be to add more padding to the right and left of the "delete" button.
Other options (which I personally would not reccomend) would include:
split the "Reschedule" into two lines and shrink the alarm clock, so it would read:
Re-
Schedule
Enlarge the trashcan icon manually (this will even them out a little more)
Manually shrink the size of the reschedule icon (your legibility will suffer)
Regardless of the route you take I would resize or edit the original files in a program such as Photoshop or Fireworks to preserve the integrity of the UI elements. Be sure to scale proportionally in those programs to avoid more distortion and stretching.

Auto adjust alignment to screen size

I am working on a BlackBerry App that has a lot of ImageButtons, LabelFields and MessageBoxes. What appears to be perfect on one screen size, seems a mess on the other. For instance, Vertical Field Managers that are neatly aligned center with LabelFields, are left/right aligned on bigger screens. Images that cover the width of the screen appear too small on larger screens. Is there some mechanism to auto-align and dynamically change images with respect to the screen size. Any ideas and documents that can help in this regard?
Here are some tips for making screens that look good on almost all devices:
Use less images. If you have to use images, use atleast 3-4 for different screen sizes. for example if you need to have an image as the screen header, use images with widths 320px, 480px and 640px. Load image depending on the width of the screen.
Do not use pixel measurements. Use point measurements instead. Most of the devices are similar in terms of physical size, whereas they have huge difference in pixel density. Using this you can have a screen which will look exactly identical on curve (320x240), bold2 (480x360) and bold 4 (640x480). If you notice, they have the same aspect ratio and similar physical size.
Do not hardcode positions. Instead use FIELD_HCENTER and DRAW_HCENTER etc for fields.
Do not use fonts with fixed pixel height. Use fixed point height instead.
If using custom fields, make sure that they can automatically expand according to device and pixel density.

Resources