I have a project and I used to add constraints with xcode interface builder. In a specific case I needed to add some constraints in my controllers source. My question is that although I have the result I was seeking for, xcode returns missing constraints warnings.
Is this something I should handle? I mean, I add constraints in the source code. Show I do something to update my storyboard in order to be aware of these?
You don't need to handle these, but if you want to get rid of the warnings, you can add placeholder constraints in IB, that are removed at build time, so you can replace them with code generated constraints. When you select a constraint, and go to the Attributes Inspector, you will see a box, "Placeholder -- Remove at build time". If you check that box, those constraints are automatically removed.
No,you dont need to , if you have added constraints in the source code than it will be executed during runtime. so you dont need to handle those warning only if you have added those constraints in the source code.
AutoLayout is a descriptive "language" which describes how the layout should be rendered (frames size, spacing etc.) into the view during the runtime. When you have missing constraints warning, in other words ambiguous layout,there is a good chance that the layout will have unpredictable behavior. In order to make sure that your layout is rendered as you wish, you should clear these warnings.
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 am new in iOS. I see a lot of disable constraints in my Main.storyboard and I want to delete it.
However, my Main.storyboard have many screen so I want to find the way to find all disable constraints then delete all.
Is it possible with Xcode?
Or any way to prevent disable constraint generate.
Any help would be great appreciate
Obviously there's the manual way, of going through all of the constraints in Interface Builder and just deleting the disabled ones.
But, if you have a tonne of disabled ones and you just want to wipe them all out, you can edit the storyboard file directly (do this with Xcode closed).
You could build a script to this, but here's the manual process to start with.
Look in the Storyboard file for lines such as: <exclude reference="egm-9S-fZb"/>
In this case, egm-9S-fZb is the id of the constraint that's disabled.
Now find a line in the storyboard like this: <constraint ... id="egm-9S-fZb"/>
Delete both of those lines and you've deleted that disabled constraint.
Note: Be aware that if you have constraints that are disabled in certain conditions, i.e., you have a constraint that's only enabled for say Compact Width, Compact Height, it will be listed with an exclude line, as well as an include line. So check for an include line before deleting the exclude and the constraint lines.
Disable constraints are generated when you create a constraint and delete it from under "Size inspector (one with ruler like)". Just remember to delete it from your scene, then you will not have disable constraint floating around...or just delete it when u see it :).
#Dace Wood and #Bao Tran already said some useful measures. I would like to add something.
If these disabled constraints are only for a specific view object, you can manually delete those disabled constraints from IB and then add new ones. Or to take risk, you can use the "Reset to suggested Constraints" or "Add Missing Constraints" seen in "Resolve Auto Layout issues" seen in bottom right part of storyboard. It is really helpful.
I'm having a problem with Xcode constraints. I'm laying out my entire layout, then selecting all the items and having Xcode 'fill in missing constraints'. Now when I run the application in the simulator, labels and textboxes are NOT in the right places. Sometimes not visible at all (appear to be off screen)
So are there any tips or tricks? Should I layout 1 item at a time, set the constraints automatically then move to the next?
Don't trust XCode and 'fill in missing constraints' feature. The best option is to set all constraints manually. After some practice it's not that hard. You need to indicate X and Y coordinates and sometimes height/weight.
I really don't advice setting constraints automatically. It usually causes more problems than benefits.
Also check the console log at runtime. It may happen that there are conflicts between constraints and you need to fix it.
Check out the official guidlines
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.
I'm trying try hide table view and move a button up the screen, which is different to my autolayout setup. Basically, I have a free version of my app where I hide things, then if they purchase I need to set the constraints back to those in interface builder.
I have a similar question open, but I think I'm fundamentally flawed in my approach.... (I've taken the advice of something who provided and answer, asking this separate question).
ObjC, revert to interface builder autolayout constraints, after adding / removing programmatically?
I can move things up fine, which modifies my interface builder constraints.
But, when the user clicks upgrade, I cannot then set the modify they again / constraints back.
No matter what I do, use visual format language at runtime, or use NSLayoutConstraint constraintWithItem outlets to copy and then modify my constraints it doesn't change. I've tried removing constraint outlets, copying those I stored at viewdidload and remove vfl constraints, exact copies of those which I added for my free version.
I was getting some vfl warnings, until I added priorities.
Is there somewhat I can dump out the vfl for everything and try and figure out the problem or can something suggest the why the approaches I have tried have failed?
Instead of attempting to replace and restore constraints at runtime, consider setting up all the constraints you will need in Interface Builder, with references to those which are state-dependent. Then, when state changes according to your own logic, activate the state-appropriate constraint(s) and deactivate the inappropriate ones. NSLayoutConstraint has an active property, which determines whether it is used for laying out its view.