Manage auto layout for imageview in tableviewcell - ios

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?

Related

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 fit different screen sizes in 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}

UIScrollView Height and width

I am facing one strange problem, using the below code I am making the UIScrollView to full screen.
CGRect screenBound = [[UIScreen mainScreen] bounds];
CGSize screenSize = screenBound.size;
CGFloat screenWidth = screenSize.width;
CGFloat screenHeight = screenSize.height;
CGRect scrollFrame = CGRectMake(0, 0, screenWidth, screenHeight);
self.imageHostScrollView.frame = scrollFrame;
NSLog(#"Scroll Height: %f, Width: %f",screenHeight,screenWidth);
The problem I am facing when the iPad is in the Portrait mode, the height will be big and width will be small, instead I am getting height smaller and width bigger (same happens in Landscape mode also).
Portrait mode the value I am getting is
Scroll Height: 768.000000, Width: 1024.000000
In Landscape mode the value I am getting is
Scroll Height: 1024.000000, Width 768.000000
Can anyone help me
The problem is that the way you're setting the self.imageHostScrollView.frame is silly. You are effectively hard-coding assumptions about the screen into the frame of a view — two things that are completely unrelated to one another.
Instead, use auto layout to pin the edges of the scroll view to the edges of the window. That way, whatever the window may do from now on — making no assumptions about what that may be — the scroll view will continue to fill it exactly.

iOS convert number to iphone 6 equivalent and 6 plus equivalent

Lets say I have a 44px x 44px UIView on the iphone 5. What's the formula for converting that to the equivalent pixel size for the iphone 6 and 6 plus??
Like would it be 88 x 88 & 132 x 132 or what?
If I have a 44 x 44 UIView on the iPhone 5, how can I have that same view on 6 and 6 plus proportionately take up the same space on the screen
+(float)convertWidthToEquivalentDeviceWidth:(float)width{
float sizedToWidthDesignedTo = 320.0f;
float actualWidth = [[UIScreen mainScreen] bounds].size.width;
float percentWidth = actualWidth/sizedToWidthDesignedTo;
return width * percentWidth;
}
+(float)convertHeightToEquivalentDeviceHeight:(float)height{
float sizedToHeightDesignedTo = 568.0f;
float actualHeight = [[UIScreen mainScreen] bounds].size.height;
float percentHeight = actualHeight/sizedToHeightDesignedTo;
return height * percentHeight;
}
You can get the width of the screen and divide it to the number you want so you don't need to do code for every single device:
viewWidth = [[UIScreen mainScreen] bounds].size.width / scaleOfYourChoice
Setup constraints for top, leading, trailing space and aspect ratio.
The leading and trailing constraints will make your view change width on bigger phones and the aspect ratio constraint will adjust its height accordingly.
Here's what you do: you set the frame to be 44x44. Then you do nothing else whatsoever. UIKit coordinates are not in pixels; the problem is dealt with for you.
Use -[UIScreen nativeScale] if you're curious how many physical pixels are inside a point. That's effectively the number your UIKit coordinates are being multiplied by to make pixels.
If you instead want your view always to be, say, 36% of the available space, use an equal widths NSLayoutConstraint with a multiplier of 0.36.

Auto-resize UIView to fit iPhone 5 screen while maintaining proportion

My StoryBoard is configured for iPhone4 resolution, and when running on iPhone 5 I'd like a UIView to get bigger (both height&width).
The problem right now is that the view is only getting higher and not wider. What should be the auto-resize configuration in order to achieve this?
You will probably need to use a subclass of UIView with the setFrame: method overridden to catch frame changes.
- (void)setFrame:(CGRect)frame
{
frame.size.width = frame.size.height; // Make the *width* always equal to the *height*. Logic could go here, etc.
[super setFrame:frame]
}
Reference the height of the screen and use some padding values to set your view frame. Below is code to set the frame of a UIView called yourShapeView:
// Get the frame of the screen
CGRect screenFrame = [[UIScreen mainScreen] bounds];
// Set padding from the top and bottom of the shape
CGFloat verticalPadding = 40.0f;
CGFloat horizontalPadding = 20.0f;
// Get the height/width values
CGFloat height = screenFrame.size.height - (verticalPadding * 2);
CGFloat width = screenFrame.size.width - (horizontalPadding * 2);
// Set the size of your shape based on the height and padding
yourShapeView.frame = CGRectMake(horizontalPadding, verticalPadding, width, height);

Resources