using Standard Values Instead of constant in Autolayout IOS - ios

I have just started learning IOS Development so my knowledge to it is very little.
I am building my layout for my app. I need my app to run on all devices . I have read somewhere that you should never use a constant magic number when creating constraints and always use standard values. I want to support my app for all resolutions
whenever I set standard value it's '0' so does this means that I have to play with multiplier values to have similar spacing
And kindly let me know when we can use constant value and when we have to avoid.

Most of the times you need to space the views evenly in the screen . This makes the view look similar on all devices (like on a bigger screen it should be equivalent to scaled version of how it looks on a smaller screen).
For Eg. If you need to space 3 views horizontally and equally on a view. If you set the height/width of the buttons to a magic number, say 100 pixels. The 3 subviews would look relatively smaller on a iPhone6+ screen than say on a iPhone5 screen.
This is when you use multipliers. Like height/width of buttons = 0.2 of superview.
On the other hand you need to use magic numbers in some cases.
For Eg. You are creating a canvas on the screen in which the user will draw with a menu panel on the left. You know the menu panel fits in say a 50 pixel strip on the left. So if you set the size of the menu panel by a multiplier, then the menu panel will become larger on bigger screens. This would somewhat nullify the advantage of having a bigger screen to draw.
This is when you use magic numbers as you know the menu panel needed to be no larger than 50 pixels.
(Note: never use magic numbers directly. Create constants like k_menu_bar_height = 50. Then use this constant wherever you need. This is much more readable and helps in the long run from a maintenance point of view)

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

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.

iOS - Autolayout/Size classes for buttons

I'm a bit confused about when to use autolayout and/or size classes for certain situations. On my initial view I have 2 buttons, one "continue with facebook" and one "register via email", stacked vertically. I initially set these to have around 70 height and 18pt font, and it looks great on the newer phones, but on the smaller ones they look enormous and combined (with spacing) end up taking up almost half the screen.
So my first thought was to make the buttons a percentage height of the main view or a subview, but things just started getting really complicated really fast: do I use percentages for the margins and spacing as well? Where does it end? It's a lot of calculations.
But I also don't see how it would be useful to use size classes here, since my app is only in portrait mode, and I can't really target say iPhone 5 vs iPhone 6 in that way.
So do I have to bite the bullet and use percentages to get nice looking buttons on all screens, or should I just suck it up and have really fat ones with a big font on the now less-used phones? And when it comes to font size, is there a generally accepted standard set of sizes (for headers and body text, for example) that looks good on all phones?
did you try use an aspect ratio constraint? if you use that then keep the distance from the edges fixed and it should squash proportionately for the different screen sizes

xcode image size isn't consistent through ios models

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.

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