Having trouble with AutoLayout Xcode constraints - ios

I have a somewhat complex layout that I want to make constraints for.
Top three all need to be equidistant from each other and equal widths and heights. Bottom three need to be equal width and heights, and also equidistant from each other.
How come Xcode doesn't have an equidistant margin option? I can't create a specific margin constraint because it wont look correct on smaller devices. Nothing I do seems to create acceptable constraints.
Thanks!

For top three you need to create 5 additional views, place them between this three views and set that they should have the same width. One of them should have some constant width(than all other will get this width as well). Than bind them all by connecting leading and trealing constraints between them. Next thing to do, is to set your views width and height. Margin that you need will be that five views that you placed before between them.
The same thing you can do with your other views, just adding some views to make them as a margins.
Is it what you was searching for? If not Pls describe your problem and I will try to solve it.

As #Dan said, UIStackView is your friend. Put the top three in a horizontal stack view and the bottom three in a vertical one. Your other alternatives are to fiddle with the Multiplier on individual constraints, i.e. 0.0 0.333, 0.667. That would require a lot of work. One other choice, if you're willing to wait to use this in Xcode 8, is that Apple has reinstated the old springs and struts style of pre-Autolayout in Xcode 8, and allows you to use that as an alternative, and/or mixed in with auto layout in Xcode 8. Try the UIStackview first, though, it's the path of least resistance.

Related

How to use auto layout so my app fits all screen sizes in swift?

I need my buttons to stay in the same positions for all phones.
I want it to look like this on all iPhones:
But when I switch to a larger size phone it does this, it also looks ugly on smaller phones as well:
That is a VERY broad question, as a lot of iOS UI depends on autolayout. I suggest you work through these tuts at at least AutoLayout. From your screen shots, it looks as if you have set a width constraint on your view with the green background instead of pinning the leading, trailing, top, and bottom to the edges.
Auto layout is, in essence, a system that performs calculations based on constraints. This means that to perform what you want, you need to add constraints to each of your objects so that the compiler knows how you want to resize your UI. But like others have said, that is a very general question. I suggest looking into the topic a bit before asking, so you can narrow down your question and get better answers.
You need to define positions for all your button or views. You need to define atleast 4 constraints for all your view. By adding constraints you tell your views where they should be placed on all screens. You define their positioning with respect to screen by adding constraints.
When you add trailing, from top and width and height constraint you tell that view should be placed in fixed position i.e. x , y and occupy defined space whatever may be screen size.You can opt for fixed width or height. Instead of fixing height and width you can also define leading or trailing constraints for the view. Constraints will adjust views frame according to screen size.
For beginning you can opt for some tutorials available online. You can check Raywanderlich here. Hope it helps.

What is the optimal way to align buttons and text? Basic, but necessary due to misinformation online

I've seen a lot of different topics and suggestions on aligning and inputting buttons/text, but the ways I've seen seem kind of risky.
What is the optimal way, for example, to add two buttons, stack them together, and have them be 10% from the bottom of the screen, and centered horizontally on all devices?
Learn Auto Layout if you haven't yet. Use constraints for achieving the following:
For centrally Horizontal on all devices: Use Center X with SuperView.
For having them 10% from bottom, use multiplier value say 0.10 .
The optimal way would be using storyboard for implementation and use of constraints in a proper way.
For example, as you suggested you want bottom space to be 10% of device height so it can be done with the multiplier in constraints also the horizontal center can be easily done with the same, so you could look for a good tutorial of Auto Layout and constraints for better understanding.
Use your Storyboard and add Auto Layout to your elements. Here is an example:
I have added auto layout
Left
Right
Top
Bottom
If I want to change the distance between the buttons, I just choose the auto layout constraint that I have created between the buttons and changes the constant value to it.
If I say that "Button 4" constant to the top shall be 40, then it´s always 40 no matter what size the phone has. If I change the constant between the buttons to 10 then it´s the same for all sizes.
Your best bet is to use Auto Layout. It takes a bit of learning but once you get used to it you can add constraints pretty quickly and easily in Interface Builder. I can't recommend any particular guide but there are a lot of good ones to be found with a quick Internet search.
Here is an example of constraints that seem to be what you are looking for:
For iOS 9, an even simpler Auto Layout approach would be to use UIStackView.
As you can see, no constraints are needed for the buttons embedded in the stack view, as the stack view lays out the buttons for you. All you have to constrain is the location of the stack view itself.
Here's an example of two vertically stacked buttons, 10% from the bottom of the screen, and centered horizontally for all devices.

Constraints for universal storyboard on iPhones

I'm struggling a bit with constraints on iOS with the differents screens sizes nowadays.
I tried google and stackoverflow to find a solution but my english does not seem fluent enough to find an answer.
I got 4 buttons verticaly aligned, the first and the last one are constrained by the edges of screens (kinda easy). But I'm really struggling with the constrains of the 2 middle buttons. I can't find a way to make them equally spaced from the left and right buttons on every size screens.
Is there something easy and tricky to make these constraints right ? Or am I doing it wrong and should I try to do it programmaticaly ?
Thanks
Add three invisible views in middle of each button, make their width equal with each other with constraint then your four buttons will be equally spaced. For the Y then you just need to pin it at your desired place.
If you are going to support only iOS 9 and higher, then use a UIStackView.
The solutions to this problem is very simple. This can be solved using the concept of "spacer views". You have to place invisible views between each red coloured view. You would be requiring 3 in your case. Then make their background as clear colour. .
Next, make their width equal and constrain their edges to the views that are after and before that view. You then define the size for red coloured view.
REMEMBER dont give the "clear coloured views" any fixed width. It would be determined by the runtime.This would solve the problem. Tell me if any more information is required.
Here is a blog post for the solution for this
http://adamdelong.com/fluid-layouts-with-auto-layout-size-classes-spacer-views-and-constraint-priorities/
This is the youtube video for this
https://www.youtube.com/watch?v=eSG-3-QpmWk&feature=youtu.be
Besides Tj3n's answer with views between buttons, you could use
A UIStackView (iOS 9!) where you use for settings Axis: horizontal, Distribution: equal spacing
A Toolbar (depends on what you want to do with your buttons) with toolbar items and flexible Space between them
Why not size classes? Apple introduced the concept of adaptive user interfaces in iOS 8 relying on a combination of Auto Layout and size classes.
If you aren't aware of what is size classes, there are plenty of tutorials available, please find one.
Summary: Apple very cleverly removed two story borads for iphone and ipad and made a single story board for universal app. No you dont have to struggle trying to apply autolayout constraint that satisfies all the screen sizes :)
Below are few of the size classes and their meaning :)
Regular width x Regular Height ----> iPad Potrait mode/ipad landscape mode
Compact width x Regular Height ----> iPhone 6 plus,iPhone 6,iPhone 5s,iPhone 5,iPhone 4s potrait mode
Compact width X Compact Height ----> iPhone 6,iPhone 5s,iPhone 5,iPhone 4s landscape mode
Regular width x Compact Height ----> iphone 6 plus landscape mode.
You can select the size classes you want to support from story board :) and start applying constraints specific to each size classes (like buttons in middle) or if you have generic one (like your buttons fixed to screen) for all the size classes.
You can deploy, remove, reuse or delete the constraints form various size classes.
SUMMARY: Buddy, If you are not using size class yet, its a high time to start using it :) There is a wonderful video on it in apple WWDC sessions 2014 i believe. Download, watch, start playing with it.
Happy coding :)
you can use equations to get this appearance.
use views' trailing points to get this.
View1.trailing = superview.trailing*(2.0f/9.0f)
View2.trailing = superview.trailing*(4.0f/9.0f)
View3.trailing = superview.trailing*(6.0f/9.0f)
View4.trailing = superview.trailing*(8.0f/9.0f)
if you make View1.width = superview.width*(1/9.0f) you can achieve what you want.
there are a lot of similar solutions for this issue. but the base is using trailing points.
It may also be done with using centerX positions of Views.
View1.centerX = superview.trailing*(3.0f/15.0f)
View2.centerX = superview.trailing*(6.0f/15.0f)
View3.centerX = superview.trailing*(9.0f/15.0f)
View4.centerX = superview.trailing*(12.0f/15.0f)
Thanks to LearneriOS answer, I solved my problem.
In order to get my wished result, I created 3 views with 10 width.
My first and my last button were already constrained. I constrained my first extra view to my first button with Horizontal spacing then i constrained my first extra view with the Center vertically in container. I then constrained my extra view to his own width and heights.
There come the important part: I did go on the constraint menus and selected the width constraint. The value inside was still 10 but I did change the priority from 1000 to 750.
Then I did copy my first extra view and constrained all of them to the nearest buttons, the same way I did with my first entra view but I removed their width constraint (to all the extra views but the first one) and constrained the extra views with the first one by plugging the: Equals Width.
Then I got my result, I hope it was clear enough and thanks again.

Xcode 6 auto layout programmatically?

My problem is on this image:
I have this problem with Xcode 6 and I would like to know if there is a way to solve this problem programmatically... I would like to write a code which adapt my view controller to all devices... Is it possible??
(I already tried auto layout manually but I am really bad)...
Seems like you've implemented too strict constraints. As I see on screenshot, you've probably set a strong width constraint for your buttons and inputs instead of equal widths or equal spacing between each other. Make your constrains more flexible. Prefer >=,<= constraints, equal widths constraints, leading/trailing constraints. You will need more of them in comparison to strict ones, but you'll get a true adaptive layout.
To get flexible equal spacing between views, you might find helpful a conception of spacer views. In short, place a transparent UIView and set some constraints to it. To know more, read apple docs. If you like videos more (like I do :)) - watch wwdc videos, they are great. For example, Auto Layout by Example or Taking Control of Auto Layout in Xcode 5.
As an example of equally arranged buttons take a look at this screenshot:
This is done with a little bit complex, but understandable constraints set:
All the buttons has the equal widths constraints for each other. All spacer views has equal width constraints on each other. Each spacer view has leading/trailing constrains to buttons or parent view leadings/trailings.
With iOS 9 you have a few more options:
instead of spacer views you can use UILayoutGuide
UIStackView allows to achieve the same layout with no constraints at all
I suggest you to watch new WWDC 2015 videos, "Mysteries of Auto Layout",Part1 and Part2
Hi #Pedro Costa its not possible here to write solution of your above problem. I am giving one link give some movement to study it. its too simple to understand.
May this will give you the idea how to set it.
AutoLayout Constraints Programatically
http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/
Home this will help.

Content hugging priority Dynamic Cell Size

Recently I've had a complex auto layout situation that has been solved (thanks to #Catalina T.). Currently I'm experiencing an issue with Content Hugging Priority. I have 4 dynamic labels that may contain huge texts so cells should fit the content. With the current constraints and priorities if I set huge texts to all dynamic labels it works perfectly (though one of them won't be displayed fully instead it will be clipped with dots). If I set the third label's text to a huge one auto layout goes crazy... I get something like this:
Whereas I want all the labels to be arranged consequently.
Demo project.
How to fix this issue?
Thank you in advance!
I managed to solve this issue.
AutoLayout should know leading, trailing, top and bottom constraints from all your dynamic views. Besides that it might happen that one view is huge and others are not, as a result you'll get the wrong arrangement. To solve this simply add Vertical Spacing for each dynamic views that are not yet connected with this constraint. Then you should change the constraint Relation to Greater Than Or Equal, so you'll simply notify the AutoLayout that you want these views to be arranged consequently.
Solved demo project.

Resources