Auto Layout: Square Image View with equal width / height - ios

The title might seem a bit complicated so I'll just show you what I want in some pictures.
So here is what I want:
[1] - http://i.stack.imgur.com/5Vckr.png
And here is how I tried to accomplish it in xCode 6:
[2] - http://i.stack.imgur.com/ags9s.png
[3] - http://i.stack.imgur.com/UhIuy.png
As you can see I tried to use size classes to snap the ImageView to the left and right in portrait view and to the bottom and top in landscape.
But somehow it is not aligned right in Landscape and if I look at a iPad preview the ImageView does not show up at all.
This probably is pretty simple but I tried for hours and still have no result.
Thank you for your help!

First of all switch mode from w:Compact/h:Any to w:Any/h:Any.
By adding constraints in this mode you will apply them for all devices and orientations.
Here is list of constraints that you need to apply:
Set lower priority (e.g. 750) for the two constraints with the dashed border (Equal Width and Equal Height to superview), and let the others have default priority of 1000.

Related

Trouble with layout on various screen sizes

My screen layout has elements placed correctly
but when I switch to another screen, eg. iphone 7 plus
or se
things change. I've set constrains, height and witdh when I suppose I should, but apparently it does not want to cooperate with me. Please tell me what constrains and heights, widths should I set.
Add below constraint
For the red and green View You need to give Equal Width Constraint.
For Blue View at the center you need to give Center Horizontally Constraint.
For the bar at the bottom You need to give Bottom distance from the Red/Green Top Constraint.

What constraints am I missing for this to work on all iPhones in Compact?

I'm new to coding and trying to learn xCode/Swift. I'm confused on how I can get these 4 imageviews to show correctly on any iPhone in compact mode. I've set the constraints in compact mode but here is what they look like in iPhone 6 Plus, 6 and 5. Can someone help me with this? I'll also add a picture of what my current constraints look like. I'd like them to be center screen with a small gap of about 5 between each box (want them close to together but not touching).
What they look like in simulator:
Current constraints:
In your case better way is to set constraints not to top, leading and trailing, but to centerX and centerY.
And set width and height of views proportional to screen width and height.
If you need fixed width of uiimageviews, you should align all views to centerX, set fixed width and height and set Y value, also if you need all views central you can set Y value to centerY. Don't forget to change '0' to current canvas value in horizantally\vertically alignment
Contrains
simulators preview
Hope this will help, ask me if you need more explaination.
Here the View.centerY = centerY and View.centerX = centerX is set for the subview(teal color view) of the main view.
Note: Set the teal view's color to default(clear) color
Result:

Xcode - Constraints on iPad - Landscape vs Portrait

I can't seem to figure out constraints and auto layout.How can I have the spacing between two objects be different on the iPad when in landscape and portrait.
I placed to UIViews side by side. When in portrait I would like the spacing between them to be 40 but when in landscape it can be larger because there is more width.
This is what it looks like in portrait mode. Which I am happy with.
And here it is in landscape. As you can see it doesn't seem to fill the screen as well and there ends up being a lot of empty space.
Any suggestions? Thank you!
you can click the small + icon next to the constraint value and select different values for another size class.
or you can change the constraint value in code depending on your orientation.
or you can add extra constraints with lower priority and then increase the priority once you are in landscape mode.
Instead of giving the spacing constraint a constant value like 40, make it a proportional width constraint that is some small percentage of the width of one of the views it is installed between, or a small percentage of the width of the overall superview. That way, as the width of the screen gets larger (as in landscape orientation), the spacing proportionately gets wider as well.
In addition to #Daniel Hall's answer, I would also like to point out that you could also pin the leading and trailing edges of the two UIView you have to their parent view and each other.
For example:
First UIView: Leading Edge 10 points from parent view, Trailing Edge 40 points from second UIView
Second UIView: Leading Edge 40 points from first UIView, Trailing Edge 10 points from parent view
It should resize itself based on these auto layout constraints once you rotate your device. Of course with this method, you run the risk of having your two UIView stretched beyond accurate proportion, so use it wisely.
I ended up having to pin it to the to the bottom and their respective sides.
View 1:
Leading: 30
Bottom: 25
View 2:
Trailing: 30
Bottom: 25
I used a >= constraint for the space between them (>= 5). If I increased the constant on any of these constraints the views would overlap. It's not the best situation because I still have a lot of empty space in landscape mode but it's the best I could come up with.

Square UImageView in a Cell of UITableView

I'm trying to place an ImageView in a Cell, I want the Image to be square and to work on most iOS phones, iPhones and iPads.
I'm using constraints to do that, I made an aspect ratio but it doesn't seem to work right.
Do you have any idea how to do that ?
As said by Vladimir in the answer Auto Layout: Square Image View with equal width / height
you need to set this constrain to your imageview
First of all try to switch mode from w:Compact/h:Any to
w:Any/h:Any. By adding constraints in this mode you will apply them for all devices and orientations.
Here is list of constraints that you need to apply:
Set low priority for Equal Width and Equal Height to superview.

Working with AutoLayout in Portrait and Landscape

Auto Layout, as good as it is, driving me crazy with constraints. I have the portrait mode designed in IB but I can't get the desired landscape orientation to work.
Notice that when the orientation changes to landscape, the spacing between the UIImageView blocks decreases and the text alignment of SAMPLE TEXT changes to right aligned.
Currently, I have added a few constraints for it to work. However, I need to keep the space between UIImageView blocks fixed, as a result of which it looks cramped up in portrait and fine in landscape. I need it to be spread out evenly in portrait and compress in landscape (like in image above). Similarly, currently my constraints bring up the text but it doesnot keep it at the center of screen and right aligned.
Is this possible at all with Auto Layout? If so, how? Help greatly appreciated.
There are several ways to approach this. One way is to enclose your 3 image views in a UIView. Give the top and bottom image views constraints to the top and bottom respectively, and give the middle one a centerY constraint. Give this enclosing view a fixed height and width, and a constraint to the top of the controller's view. Make an IBOutlet to the height constraint for this enclosing view, and change it on rotation. In the example below, I gave it a height of 350 in portrait:
-(void)updateViewConstraints {
[super updateViewConstraints];
if (self.view.bounds.size.height < self.view.bounds.size.width) {
self.heightCon.constant = self.view.bounds.size.height;
}else{
self.heightCon.constant = 350;
}
}
As for the label, the easiest way is to remove the constraints you have (bottom and centerX), and add trailing and centerY on rotation.
Yes, it can be done with Auto Layout. If you want the spacing between views to increase and/or decrease depending on orientation, you can use the Less Than or Equal or Greater Than or Equal relation (instead of Equal) for the constraint, which allows a distance to grow or shrink:
Play around with that and you should be able to get what you want.
Yes, it is definitely possible to do this with Auto Layout. Through a series of steps, that I think might be too long to post as an answer, you can retain the spacing between your ImageViews and keep the alignment of the text the same.
Essentially, you will have to pin a corner of each ImageView and remove some constraints so that it doesn't automatically compress the spacing when you change the orientation.
Full explanation on how to do this (pretty much exactly what you are asking for) is explained in this tuorial. You can find it about halfway through the page.
Hope this is what you were looking for.

Resources