How to fit different screen sizes in ios - ios

I know this question seems to be a old-school one, but I can't find a nice answer to it, there is the thing:
The logical screen resolutions for iPhone5, iPhone6 and iPhone6+:
iPhone5 320 x 568
iPhone6 375 x 667
iPhone6+ 414 x 736
Assume that I have a square UI element at the center of the screen, and there is three ways to constrain its width(&height) over different devices:
width is constant
width/screen.width is constant
screen.width - width is constant
these three different ways of constraining result in three different appearances:
Picture 1: 3 different appearances
I think the 2nd way of constraining (i.e. middle column in the Picture 1) is most natural from designer's point of view but it's least natural from programmer's point of view. I need to check device type and calculate dimensions and use different images for different devices(autolayout can't help in this circumstance)
1st way seems very natural to implement, but it sucks in practice, especially when dealing with text font sizes.
3rd one also need to judge which image to use programmatically, because three UI elements in three different devices do not match the simple #x2 #x3 scale factors.
Is there any work around to easily implement 2nd way of constraining?

You can just give Horizontally in Container and Vertically in Container constraints to view which is in centre if ViewController and if needed then give Height and Width of View itself.

If you are using storyboard you can easily calculate the 2nd way you want. You can give proportional width with the superview.
As you can see in the image
you can give multiplier to your views.
Or you can create a width constraint in your code such as:
#IBOutlet weak var widthConstraint: NSLayoutConstraint!
and you can update your constraint in your code.

Use Macro's
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_RETINA ([[UIScreen mainScreen] scale] >= 2.0)
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#define IS_IPHONE_4_OR_LESS (IS_IPHONE && SCREEN_MAX_LENGTH < 568.0)
#define IS_IPHONE_5 (IS_IPHONE && SCREEN_MAX_LENGTH == 568.0)
#define IS_IPHONE_6 (IS_IPHONE && SCREEN_MAX_LENGTH == 667.0)
#define IS_IPHONE_6P (IS_IPHONE && SCREEN_MAX_LENGTH == 736.0)
if(IS_PHONE_5){//TODO}
if(IS_PHONE_6){//TODO}

Related

Manage auto layout for imageview in tableviewcell

I need to give auto layout constraint for ImageView in this type of layout all other stuff are perfect but ImageView are not upto the mark.
All three boxes in image are ImageViews First from left, I tried storyboard layout Leading, Trailing, bottom and Width fix and for second imageview-> Leading, Top, Trailing and height fix. Bind their respective width and height constraint and programatically doing this:
imageView1WConstraint.constant = 310 * WIDTH_RATIO;
imageView2HConstraint.constant = 156 * HEIGHT_RATIO;
P.S: WIDTH_RATIO and HEIGHT_RATIO are macros and defined like this for iPad.
#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width
#define SCREEN_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define WIDTH_RATIO SCREEN_WIDTH / 768
#define HEIGHT_RATIO SCREEN_HEIGHT / 1024
I tried this all but imageview are different at runtime, can anyone help me in achieving this?

How to find View frame for IPhone 5 and IPhone 6 and IPhone 6+

I have an application where I am adding one view with certain frame programmatically, it is okay for iphone4 (320 * 480) but How can I calculate frame size for IPhone5 and IPhone6 resolutions with given autoresizingmask value , Thanks in Advance.
To create programmatically you can use screen bounds to calculate size of the device
CGRect screenRect = [[UIScreen mainScreen] bounds];
screenRect.size.height == 667 width = 375// iphone6
screenRect.size.height == 736 width 414 // iphone 6+
screenRect.size.height == 568 width 320 // iphone 5
I forgot to give autoresizing mask. Adding that fixed it.

How to measure the pixels provided in iPhone UI

Currently I am working on an iPhone application. I find some difficulties in fixing the UI layouts.
Is there a tool thats measures the pixels given in my application? Something like a ruler in Microsoft Word or Photoshop around my application which allows me to find the measurements in my app.
Thanks
If I understand correctly, you'r looking for a way to better understand the current (and desired) positions of the UI elements in InterfaceBuilder (whether you're working with xib files or storyboards).
If that's the case, there are two things you need to know:
If you select any object on the screen and press the ALT key then drag the mouse cursor (without pressing the mouse button), you'll see the distance between that element and any other element that you're interested in (the one your cursor is currently pointing to). This makes the positioning of elements on the screen very straight forward and is relatively similar to the rulers you described.
In addition to the previous tool, you have the Size Inspector tab on the top right corner of InterfaceBuilder, in which you can see the sizes and positions (in points, not pixels) of the current element you're editing. The display of this tab changes whether you're using auto-layout or autoresizing masks.
I hope this helps. Good luck!
Screen height and width macros:
#define SCREEN_WIDTH ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.width : [[UIScreen mainScreen] bounds].size.height)
#define SCREEN_HEIGHT ((([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) || ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)) ? [[UIScreen mainScreen] bounds].size.height : [[UIScreen mainScreen] bounds].size.width)
these macros give SCREEN_HEIGHT and SCREEN_WIDTH whenever you use them for the current Iphone or Ipad device
If you want the scale then you can check the scale like so:
[UIScreen mainScreen].scale;
Point coordinates multiplied by screen scale = Pixel size, so for an Iphone 6+ this is how this would work:
SCREEN_WIDTH * 3 = Pixel width
SCREEN_HEIGHT * 3 = Pixel height

Change size of imageview, sprite, label, etc. depending on screen size iOS

I've come across this problem when creating UIImageViews, on the iPhone 6, it fits nicely, but on the 4s, it is huge!, is there a way to add some logic in the ViewDidLoad to change the px size of whatever it is, whether it's a sprite, image, label, etc. ?
Just use auto layout and size classes (You access them in IB down and select the sizi class for iPhone)
There are a few ways to do this, you could use auto-layout, or you can take a different route. If you chose not to take auto-layout, I would recommend do this, however the down side is that there is a lot of programming.
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
if (screenHeight == 480) {
// do iPhone 4s stuff
} else if (screenHeight == 568) {
// do iPhone 5, 5s, 5c stuff
} else if (screenHeight == 667) {
// do iPhone 6 stuff
} else if (screenHeight == 736) {
//do iPhone 6 plus stuff
}

xcode simulator iphone screen size

I put this code in my app:
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
NSLog(#"screenWidth : %f", screenWidth);
NSLog(#"screenHeight : %f", screenHeight);
The I start the simulator with Iphone 3.5 inch.
The log prints 320.000000 for the width.
However, when I then change the device for the simulator, selecing 4inch, it stills shows the same width. Why? Whats wrong. It is supposed to be 640 px in width for Iphone 4 and above, doesnt it?
In the simulator screenWidth will be always 320 ( right now iphone4-5 ). You can check the scale with [[UIScreen mainScreen] scale];
scale will be 2.0 if you running on retina iPhone.
According to the Apple Docs, that value is the "default logical value". If you want to know the actual size, take a look at [[UIScreen mainScreen] scale]. For retina devices, this value is 2.0, so do [[UIScreen mainScreen] bounds].size.width * [[UIScreen mainScreen] scale]
no the width is always 320 POINTS / DPIs -- what you mean is that it really has 640 PIXELS (which are device dependant)
Calculations are always based on device INDEPENDENT points
UIScreen offers a scalingFactor that translates dpi <> pixels

Resources