I have static data on my Screen. I have taken labels to display it, but when I am going to apply constraints on it for iPhone 6 then it displays data in single line at preview screen. Please send me solution which constraints I have to add.
You can do the following:
Set height constraint for multiple label
Select it throw builder
Change 'Relation' property to 'Greater Than or Equal' (to automatically resize content depend on text)
Set 'Lines' property of target label to '0'
like so
Basically, you need to provide each element's height,width,x-position,and y-position.
If you are not sure, just drag the elements to wherever you want them to be (be careful about those alignments), and use 'Add Missing Constraints' or 'Reset to Suggested Constraints'.
Though I highly don't recommend using them, you may end up manually modifying the constraints by yourself.
You need to be more specific so that we can offer more help.
Related
My question is that, can I trust on
Resolve autolayout issues’ - “Add missing constraints”
option(as in the attached Screen shot), which automatically adds constraints to the objects present in the storyboard?
I used this and tried running the app in all screen formats and it works fine, so can I continue using this or is it wrong to consider “Add missing constraints” for the constraint design. I’m new to auto layout and any kind of response, explaining this concept will be appreciated. Thanks
Utilizing the automatic constraint system is a bad idea. Most of the time, it won't work dynamically for all screen sizes. It generally adds constraints so objects appear correct in the current resolution you're designing in.
For example, it may pin a label you have placed in the center of an iPhone screen based on the distance from the left edge of the screen instead of the X value. That distance from the edge is going to stay the same when you run it on an iPad and it's going to be significantly off-center to satisfy that constraint.
However, depending on the situation, it could pin them correctly (IE leading edges to the super view instead of a constant). You can use the automatic constraint system for suggestions to reference what you need to add still, but I would not rely on it for dynamic UI.
Spend your time learning autolayout instead of throwing darts in the dark, it's really not as intimidating as it seems!
No you should not trust. Add missing constraints will add constraints that are missing. It will not add constraints that's your design actually want.
So I suggest you to understand what constraint you'll require to complete UI.
`For every control, Compiler need to know its : x,y,width and height.
For example : You drag & drop UILabel on your xib. Now you add top space constraint. So compiler will give warning. Need constraint for : X position.
On above example width and height will take according to text of label. Now you had given top space so its y position is known.
But for X you didn't give any constraint. In this case if you use Add missing constraints. compiler will add constraint for x position according to your placement. It may be your require constraint or may be not.
No iT just add the required constant so may be they are fixed so remove all constraints and try again.
Just like what the others say, "Add Missing Contraints" will always give the result of the layout that you desired. It's best that you learn to add constraints manually. But, there are times that you can be lazy for a couple of seconds by using that method but only for very simple layout.
I'll just share my thoughts about when should we use this method.
I use "Add Missing Constraints" when:
My layout is very much simple, it's like I know that when I do it automatically will yield the same result as doing it manually. This help saves a lot of time.
I am setting up constraints manually, but sometimes I don't know what constraints I am missing because the object that I am setting the constraints still shows red lines(missing constraints). This is just my purpose of learning.
I'm in my storyboard, and applied a constraint by control-dragging from one view to another, to replace another constraint that doesn't make sense anymore. So I select the view, go to the "Size Inpector", hit the old constraint, and then the delete key.
The annoying thing, is that it just clears the "Enabled" flag, and grays out the constraint.
And yes, I am using size-classes... but here's the thing, I don't have this constraint used on any other size classes. In fact, currently there are no variants of views, constraints, etc. Everything is on "Any / Any".
So, I'm wondering if there's a way to make the Delete key actually work here. Is my only recourse going to the Document Outline, and finding my constraint in there? It just seems like there should be a more simple way to ditch the constraint, but in the Size Inspector.
If you're using size classes what often happens is that when you delete a constraint, it merely removes it for that size class.
However, there is a clear visual hint given when you do this: the constraint icon goes grey and is easily identifiable in the document outline, from where it can be selected and removed completely.
I'm not sure whether this has changed over various versions of XCode, but I find when I select it in the inspector, it does not remove it unless you are on Any/Any.
I have a layout of UIButtons that looks like this:
The horizontal spacing between the buttons is 8. I would like to change this to 4. Of course, I could change them one by one, like this:
But there are a lot of buttons and I don't really feel like doing that.
I tried selecting multiple buttons and then added a horizontal space constraint of 4 but that just adds constraints. It doesn't update the old constraints. This creates conflicting constraints with the old ones.
I didn't see anything to solve this in the Editing Auto Layout Constraints documentation.
Just use Command + click to select multi constraits,
like the gif
One of the way is to create spacer views, With each view width equal to the first one. Set your Button constraint to leading and traling to the spacer view.
and if you change first width of spacer view it will change all othes view width.
I am agree with the answer of #Leo and up voted as well :)
Just adding to it, if someone would like to make it more easier, he can use the filter option given below the constraints.
For example to find all the leading constraints one can search for " = leading" word or "trailing = " for trailing constraints or other similar search string.
You can see this from the GIF as well.
You have to do that one by one. There isn’t any way that you select all of them an edit their values.
You just have to select all button and move where you want them and then you have to just update the constraint.
I have been learning Xcode and iOS and things are going well with coding but I am confused about a few options in interface builder.
It seems that when I add a contraint I can choose to "Update Frames" and then either "Items of new contraints" or "All Frames" but it seems that I don't always need to do this... sometimes I do and sometimes I don't - I don't think I truly understand the concept. It seems to be optional. When is the case to choose it or not?
Once a constraint is added, then should that be it?
There is also a context menu (Resolve autolayout issues) (3rd item on the bottom on interface builder, represented by a triangle between 2 vertical lines) which also allows me to Force Update of constraint or update of frame. Why is this needed ? I notice the options inside are usually disabled so I presume this becomes active when there are issues ?
I have tried searching the internet and I can't find anything specific; the more I read, the more confused I seem to get.
Also, a sanity check: a frame is the bounding box of every single control (view) not just the viewcontrol, so every view (control) as its own frame. Is this correct?
I'll try to explain the concept of constraints and storyboard representation.
Constraints - are used to determine the position of UIView elements inside your view. For example, the vertical distance from top of the view to UIView element top. When you add constraint using the storyboard it plays 2 roles:
Positioning the element inside a view when your app runs
Positioning the element in the storyboard representation itself
Update Frames - is used when you want to update storyboard display so that it fits the currently applied constraints. When the display of the storyboard does not match the constraints a warning will appear.
Add missing constraints / Update constraints - will create/change the constraints applied on UIView element to match what currently presented in the storyboard. Kind of the other way around from update frames.
This answer is not a replacement for Apple's Auto Layout Guide
You do not have to click on "update frame" every time you add a constraint, if you do, it will update the frame of the view to whatever it should be based on the constraints. Yes, every view has a frame and it is the rectangle relative to the views superview.
More often than not you'll probably click on update constraints (Shift + Command + =), this will allow you to resize views that are already constrained, and have the constraints updated to match.
I'm using Xamarin Studio for iOS development and I've just started to use the Xcode Interface Builder and it is giving me headaches. My Xcode version is 5.1.1(5B1008) and I'm on Xamarin Studio 5.5 build 227 with Xamarin.iOS 8.2.0.193 on Starter Edition.
I'm not able to understand how to use the Auto-Layout functionality and I've read tutorials, most say the say, drag element into view, click on Pin icon, set constraints and be done with it.
But the elements I add don't end up looking as I want them to be.
For example let's say I want to create a view with a scroll view in it and that will display labels and text fields in it as if it were a form, by dragging-dropping element imagine I end up with something like this:
The layout I desire is for every label and text field to be 11 dps apart from the left and right, no matter the orientation, also each needs to be 11 dps apart from each other, with the initial label to be 50 dps apart from the top of the scroll view.
Every element should be the same height (20 dps) always but their width must change according to the left and right constraints.
That's the end of my layout definition.
Now let's start with defining the left and right constraints along with the height. What I do is to select all of the label and textfields, I pin the left and right and set their height to the same value:
But what I get looks like this:
The resulting layout looks nothing like I wanted, the width for every element is set to the same size and the right values don't make sense, I stated 11 dps but I get values like 187, 173 and 228.
Even if I set the top and bottom constraints, the right constraints keep looking the same:
How do I fix this? I've deleted and started from scratch several times but I can't find to understand what am I doing wrong. Some of the technical language challenges me sometimes (I'm not a native english speaker) so I don't know if I'm interpreting something differently.
Some of the links for the tutorials I've looked into are this, this and this.
You need to ask Interface Builder to update the frames of those elements. All those yellow lines/numbers are constraints that are currently not satisfied - the frames of the elements don't match what the constraints require.
However, it's often better to make sure you have all your constraints set up first, before asking IB to update the frames.
Here, I've set up all the constraints you described, but I haven't asked IB to update the frames:
Here, I've asked IB to update the frames of all views in the top-level view:
If I select the subviews, I can see that the constraints are now shown in blue, meaning they are satisfied: