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
Related
I am trying to figure out how to start using stackviews and running into issues where everything looks great in Xcode Preview, but then when I run it in the simulator or on a device it looks completely different.
Here is my Xcode storyboard:
And here is how it looks in the simulator:
I am assuming it is an issue with my constraints but I am not sure where to start.
EDIT: I have resolved the initial constraint issues but still doesn't seem to be working. The date does not have its own constraints, only the stack views:
EDIT: PGDev solution and simulator result
I have created the same hierarchy as yours and it is giving me the expected result.
Below you can find the attached screenshot of the storyboard as well as the output.
Storyboard:
1. View Hierarchy
2. Outer StackView Properties
3. Inner StackView Properties
Output:
Let me know if you still face any issues regarding this.
At first look it seems the constraints have not been set properly.
Also it looks like there are a few errors with the auto layout constraints, that is why there is a "red arrow" beside the Updates scene. Resolve the issues first and hopefully you will get the desired result you are looking for.
There are two things needed to be resolved in order to resolve view issues with storyboard and UIStackview
1) You should provide minimum number of constraints to make view proper on storyboard
2) You should resolve conflicting constraints in your views (In your case I see the red button which shows there are conflicting constraints)
2A) You should also resolve the intrinsic content issues with content priority and ambiguity described here
The main problem is that I tried command+drag to the view and selecting "align horizontally in the container", but for some reason(I was doing this on the iPhone 7) when I checked the other devices(such as the iPad Pro) they were not aligned. Then I tried adding constraints, which also did not work. My question is how in the world do you center a label without using Swift?
First thing first: "Center Horizontally in Container" is a constraint.
My initial guess is that you’ve neglected some sort of ambiguity or possibly created a constraint which conflicts with (and overrides) the centering constraint. If this is the case, you should see warnings telling you what the problem is.
As long as there's no vertical ambiguity, and no conflicting horizontal constraints (leading/trailing space), your label should be centered—no Swift code necessary.
I get the impression that you’re fairly new to Xcode, so if you don’t know already, you can check the existing constraints on an object in the Size Inspector, denoted by the ruler in the right sidebar. It’s quite possible that there’s an old constraint you forgot to remove, or one you added but don’t need.
Another thing to note: I’ve experienced some layout issues when switching devices in the new Interface Builder in Xcode 8—it’s just a bit buggy in my experience, especially with regard to Stack Views. If you have doubts about how something looks, I’d recommend running the app in Simulator for some devices you don’t have on hand to check it out.
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.
What is causing them? How to get rid of them?
Create a storyboard, add some views. There are no warnings, so store/commit it.
When you open it on another dev machine, there is a chance, it will complain about "misplaced views". You can do the suggested quickfix to resolve it, but if you go back to the first machine, it will complain in the opposite direction.
I think it has to deal with the status bars, in this screenshot you see y=64 instead of y=44, but why!? It's the same project on both machines.
Anything I can set to make it behave the same everywhere?
1: Your other developer may have a different xCode with different set of warnings.
2: xCode constraint warnings may look mysterious but actually it's doing it's best to keep you alerted. When autolayout can not decide where a view should be placed, like left or right side of the parent view THEN it will try to warn you once by putting it on the left side then once by putting the view on the right side. Same goes for warnings..
DO THIS: before you end working, go through all the Size Classes and make sure you have 0 warnings left in any of the Size class.
BTW: that top constraint is sometimes sticking to a status bar height (20px) and sometimes not. Stick it to something else. Maybe setting the statusBar settings as "Inferred" would help.
How to work your way through the size classes
You start out with Any x Any size class -> setup all the constraints -> now start switching size classes -> if everything fits (no warning) even on the smallest device in Landscape mode THEN you are ok.
If something not fits or you want a bottom view bigger UIView on iPads for example THEN you switch from Any x Any to Regular x Regular (or where the warning is) -> and alter the same constraint just for that specific size class. "Install constraint" the UI says. U can add custom UI elements too btw.
See image, I have a bigger height for the bottom view in Regular x Regular (iPad landscape/portaite):
The result will be that now and in the future you are almost 100% adoptable to any device size that will appear on the markets = less maintenance time :)
vote up :)
This must be a dumb question, but nevertheless I ask it. I don't have to explain (I think) that I'm just beginning with Xcode.
I have created this view in IB:
And this is the result in the simulator:
What can I do to fix this. It doesn't matter what device I choose (eg. 4S or 6) and device options are in sync (= same in IB and simulator).
NB: It is a new project and I have reset the auto-layout constraint to suggested settings.
You need to ensure your Auto Layout constrains are accurate.
Make sure the view a controller is selected, and choose "Clear constraints"
Select each of these items and choose "center horizontally in container"
Select each of these, and add a constraint linking each to the one above. Note Xcode will fill in their current positions as default values.
Auto Layout tip: name your items in the document outline (the left in IB). Then when you examine constraints, you will be able to see which ones they are attached to by looking in the size inspector in the right.
Watch this video and get familiar with Auto-Layout.
https://www.youtube.com/watch?v=G53PuA_TlXk
Letting Xcode use "suggested constraints" is nearly always useless and won't work like you expect. So you'll either need to position all of your elements programmatically by changing their frame, or, you need to set up proper auto-layout constraints.
Your problem is that you are not using Autolayout functionality. I Would recommend reading about it, especially if you are new to iOS Dev. You really can do a lot if you are using IB (Storyboards) - check out this tutorial is should get you started
Autolayout is a system that helps you create apps for all different device dimensions.
You need to go into attributes inspector for the view controller (right sided panel) go Simulated Metrics(should be at the top) -> Size and change it to "iPhone 4S" or "iPhone 5".