Constraint constant based on screen height - ios

So I have a view that contains some elements (Label and Text View) that are 8px spaced between each other. However, added together, the height of the view is too big for some smaller screen size. I want to know how I can change the constraint constant (currently at 8px leading and trailing between each element) based on the screen height.
For example, for a iPhone 8 Plus I would want a 8px spacing. For a iPhone 8 I would want a 7px spacing. For a iPhone 4s, I would want a 5px spacing.
Showing how to do this on a storyboard rather than code would be super helpful.
Look at picture for a better understanding

Make it in UIScrollView instead of UIView.
Anyway, you will need to scroll content when the keyboard appears. Otherwise, a user will not see what he writes in the field.
More than you will not have to make different padding between elements.
BUT. If you want to make different paddings yet, try to make depending on
UIScreen.main.bounds.height

Related

How can I center four buttons horizontally and vertically?

I would like to center four buttons horizontally and vertically. Each of the buttons shall get a background image later, so I think it's important that the aspect ratio stays more or less the same, otherwise the pictures won't fit onto the button.
A screenshot of what it should look like:
All buttons have the same size and should take up as much space as available (small margin, 10px or so, not more if possible). I don't know how to set the constraints that the application looks good on all devices (iPhone 5s and iPad 10.5").
The application must not necessarily run in all orientation modes.
How do I have to set the constraints?
This problem can easily be reduced to the problem of making a maximum square centered in its superview. You can do that easily with just four constraints:
The rest — the grid of buttons — just falls into your lap. For example, make my blue view a UIStackView containing a two UIStackViews, and there's your grid. Or you could do it with explicit constraints yourself.
If this app can rotate, you might need two sets of constraints, one for when the width is less than the height, the other for when the height is less than the width. You would then have to swap them in code. But that's another issue, and is not difficult.
Add them to UIStackView, for placing them horisontally use horisontal value for axis property you can customize it with distribution, alignment and other properties of UIStackView.
It's the easiest way

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.

Universal layout constraints

I have an app which I've designed in the width:any height:any universal size template in XCode (Swift). Basically, I have a custom cell, with its own uitableviewcell class too, and two labels inside of it which display array data. Anyway, I am struggling to add the correct restraints etc, so that the labels and text inside them display properly on all iOS devices. Even though I have changed the font to be able to go to 0.2 scale, there's still sometimes overlap or text clipped in the label on smaller iOS screens. It's probably a simple thing to resolve. So can anyone help me make this layout look the same, or the equivalent, on all iOS devices and all orientations too please? Here is my design:
The fact that your labels are overlapping each other is probably that you don't have the proper width constraints.
This is what I will do, taking the left label as an example:
add constraints to the left, top, bottom margin
add a width constraint to the parent view and making it 50% of the parent width
You will need to do the similar for the right label replacing the left margin constraint with a right margin one.
Now both the labels only take 50% of the width, then they will not overlap any more guaranteed.

Cannot get view to use entire screen width on iPhone 6+

In my storyboard I have a MapView that I would like to use the entire width of the screen. I created two constraints for the leading and the trailing space and they are set up as follows:
On the iPhone 4S - 6 the view works as expected, with the map using the entire screen width. But on the 6+ I can see that the map isn't using the entire width and has about 5px of padding on both sides, as seen here - it's hard to see with the background being white:
How can I make this mapview use the entire width of the screen for the 6+?
Edit the constraints so that they're not using the margins (do this in the size inspector for each constraint, from the item drop down) then set the constants to 0. It looks like the margins are bigger on the 6 plus, so using -16 to offset isn't giving you the right effect.
Only use constraints relative to the margin if you actually want a margin. I've no idea why this is now the default, it's stupid.

iOS autolayout with size classes does not differenciate landscape for 3"5 and 4"?

here's what I've been struggling with for the past hours.
I have a view controller that contains a few labels, a picture, and a tableview. I'm using autolayout/size classes in order to use only one storyboard for the various devices supported (meaning: all size of iphones and iPad).
Noew everything is working, except one thing : the settings in "width compact x height compact" are used for 3"5 and 4" iPhones in landscape. So either I say that the table view takes 480px and there's a whole lot of space on iPhones 5/5S, either I set it at 480px but then I cannot reliably align items vertically in my cells.
How come there is no way to say "this view should always takes the full width" ?
Any solution, even workaround, would do. Thanks a lot for your time !
There is no distinction in size classes between 3.5", 4" and 4.7" devices. They all have the same size classes deviced for horizontal and vertical, and behave the same way on rotation.
You can define a view to take all the available space. Explicitly set the bottom and top views to be attached to bottom and top edges (respectively) with constraints, and set the middle view (in your case, the table view) to take all the space in the middle by setting constraints on top with the top views, and constraints on bottom with the bottom views. Now, because top view is attached to top and bottom is attached to bottom, you will have the middle view grow and shrink as needed.
There are a couple of ways to get percentage based sizing.
Link the width of your target element to the width of another element.
Pin your element to its Superview and use the Multiplier setting instead of Constant.
Say you want to set a TitleLabel to have a relative width compared to another element.
Example for method 1:
Ctrl + drag from TitleLabel to another Target element (such as its Parent View), then choose Equal Widths, you can then change the Multiplier field in your Constraints settings to 0.5 if you want 50% width.
Example for method 2:
Pin the appropriate edges of TitleLabel to either the Center, Bottom or Trailing edges* of its Superview (Editor -> Pin -> X Space to Superview).
In the Constraints Settings, make sure you have TitleLabel in the First Item field, and the Superview as the Second Item.
*because the Top/Leading edge of the Superview will have a 0 value, a multiplier against these won't work, as 0 x 0 = 0.
I found method #1 doesn't work well in Table Views, so you may have more luck with #2.

Resources