Positioning views dependent on device height in iOS - ios

I'm switching to autolayout and I'd like to position views relatively to height of device. How should I setup constraints to satisfy such condition.
I have nice layout for iPhone 5 but for iPhone 6Plus I'd like to move "red" to position of "gray":
All my current constrains:

One idea might be to place the username, password and login items on a uiView with a clearBackground and then create a constraint for that view and its superview and create an outlet to it. You could then detect which phone you are using in code and modify the constraints programatically in willLayoutSubviews.
if ((int)[[UIScreen mainScreen] bounds].size.height == 736)
{
// This is iPhone 6+ screen
myConstraint.constant = 150;
} else if ((int)[[UIScreen mainScreen] bounds].size.height == 568) {
// This is iPhone 5s screen
frameRateLabelHeightConstraint.constant = 100;
}
There will be a better way to do this in Autolayout no doubt but I do find it a bit confusing so I have used this method in the past.

Here is a sample project uploaded # One Drive. Here are the sample outputs on various versions of iPhone Simulators...

As I see you want to put your form to the center of view. So I think the best solution will be to put your form elements on another transparent view and add to this view centerX aligment constraint and centerY aligment constraint.
you can check another way here

Related

ios autolayout does not change button frame position

I have one view, and I set this view on autolayout. When I change the button frame/position in 4s iphone, I am not able to change it. How to change the button position in ios 4s
note without changing the autolayout property ?
Here is my code :
CGRect screenBounds = [[UIScreen mainScreen] bounds];
if (screenBounds.size.height == 568)
{
// NSLog(#"Our App is running in iPhone5");
}
else
{
//NSLog(#"Our App is running in iPhone 3GS, 4, 4S");
[btn_next setFrame:CGRectMake(12,100,120,45) ];
[getTime setFrame:CGRectMake(151,361,89,162) ];
[picker_AMPM setFrame:CGRectMake(245,304,55,162)];
[img_blowline setFrame: CGRectMake(0,418,320,6)];
}
You can do it by :
Set translatesAutoresizingMaskIntoConstraints = YES, then calls to setFrame: at runtime are automatically translated into new constraints.
translatesAutoresizingMaskIntoConstraints will also cause new constraints to be added based on the view's autoresizingMask.
More details are in the section "Adopting Auto Layout" in Cocoa Auto Layout Guide.
This is because you need to change the constraints of the button, not it's frame. To do this you need to make an outlet for your constraint and change it in code and changing the constant property of the constraint. The code would be something like this:
self.myConstraintX.constant=newX;
self.myConstraintY.constant=newY;
[self.view layoutSubviews];
[self.containerView layoutSubviews];
One important thing to note is to make sure this code is not repeated.

cropping a viewController to fit iphone screen size

So I have a viewController viewController.m which is 320*568 but I would like this to change to 320*480 if the iphone is an iphone 4. I have the correct code to show if it is an iphone 4 like so:
if ([[UIScreen mainScreen] bounds].size.height == 480)
{
//crop view controller dimensions to 320*480
}
But I would like to do this without squishing any UIImageView or UITable inside the viewController like it does when you edit the simulated size height to "Freeform".
The way you doing above will waste your time. The best option for you in this case is the autolayout.
You should define your own constraints so that subviews including UIImageView and UITableView fit always on their super view which is the view controller.
Here is a good article to get you started: http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

Contents clipped using autolayout in 3.5 inch screen

i am using autolayout in my project. I added the contraints from the bottom to the top. Everything works fins in 4 inch screen . But screen is clipped when the project is run on 3.5 simulator.
Looking at the screenshots, it looks like there is no space to fit all your elements.
One way to go about it is, dock the elements at the top of the screen using the Top Space To Superview constraint, and dock the elements at the bottom using the Bottom Space To Superview constraint.
When you do this, if there's not enough space available, you'll probably see some of your subviews overlapping around the middle.
You can consider solving this by using a UIScrollView as a top level subview and add all your subviews to the scrollView. The content height of the scrollView will be calculated based on your constraints.
You can also manually set the contentView height after viewDidLayoutSubviews is called.
Personally I find that sometimes I can't seem to get Autolayout to work exactly as I want. So its worth putting a check in your viewDidLoad to manually move objects around a bit if the screen size is a 3.5 inch screen like so:
In my example I will be using a Button:
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
CGSize result = [[UIScreen mainScreen] bounds].size;
if (result.height == 480) {
// 3.5 inch display
button.frame = CGRectOffset(button.frame, 0, 89.0f);
}
if (result.height >= 568) {
// 4 inch display
// .....
}
}

Resize UITableView for iPhone 5

How can I adapt an UITableView height to iPhone 4-inch screen? I have the following configuration, which works for iPhone 4. However on iPhone 5 (see image below) there is a blank space because the tableview does not rezise. The tableview is scrollable.
I've autolaout enabled and I tried defining horizontal and vertical spacing constraints for all views and the tableview, but I get the same results (see image below). Any suggestion?
This viewcontroller is placed inside a placeholder since I created a custom menu. I tried settting the constant of the placeholder in the parent view with the following code. Also not working, the tableview is clipped.
-(void) viewDidLoad{
if([[UIScreen mainScreen] bounds].size.height == 568) //iPhone 4inch
{
NSLog(#"iphone5");
self.dynamicTVHeight.constant = 465;
[self.placeholder needsUpdateConstraints];
}
else{
NSLog(#"iphone4");
self.dynamicTVHeight.constant = 375;
[self.placeholder needsUpdateConstraints];
}
And these are the constraints of my parentview, which implements a custom navigation menu. I did two tests:
With the vertical and horizontal spacing constraints:
And with the height constraint in my placeholder:
You need to set the bottomSpaceToContainer (or Bottom layout) to 0.
Make sure that there are no warnings or errors in your nib file (indicated by the yellow or red sign on your UITableView). If there are none, make sure you have all 4 constraints needed to fully describe your UITableView's position.
What you want :
To compare iphone 4 and iphone 5 :
Hope that will help =)

Resizing a UIImageView programmatically

I have tried several different solutions to this problem with no success.
I have a UIImageView (created on the Storyboard) that has multiple Gesture Recognizers on it. I have trying to resize this UIImageView based on screen size to take advantage of the 4 inch display.
It appears that since the Imageview was built using the storyboard, I can't change it programmatically???
My current code looks like this:
//determine screen size, set scoring button width based on device size.
CGFloat lCurrentHeight = self.view.bounds.size.height;
if (lCurrentHeight == 568.0) {
CGRect _redHeadImageFrame = _redHeadImage.frame;
_redHeadImageFrame.size.width = 220;
[_redHeadImage setFrame:_redHeadImageFrame];
}
Any ideas??? Thanks.
If you are using autolayout you have to change withConstraint of the image, because manual changing of the frame will not work in this case.
EDIT
You can create IBOutlet for the width constraint as you do it for other controls - just select constraint in IB and move it with right button to your header file. Than in code change constraint:
[self.imageWidthConstraint setConstant:100.0];
yes you need to use like this,
CGFloat lCurrentHeight = [[UIScreen mainScreen] bounds].size.height; //it will give 568
CGFloat lCurrentHeight = self.view.bounds.size.height;//it will give 548,in your case that condition failed.
There's no difference in resizing UIViews created by code or by the storyboard. It's exactly the same: setting the frame of the view, as you are doing.
What might be happening:
Your if condition it's never true. Have you checked with a breakpoint if the code inside it is even called? Maybe there's some error in the float direct comparation. See this.
Also, see this for a better understanding on how to check if it's 4 inch display.
If the setFrame: code is actually called, check if you are using auto-layout in your view. Maybe there's a constraint in the UIImageView. In this case, you should play with this constraint, in order to resize the view correctly..

Resources