StackView different between Xcode Preview and Simulator - ios

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

Related

Why might Autolayout constraints not appear at runtime?

I am trying to create a custom subclass of UITableViewCell that displays an image and a few labels to the right of the image. I have my UIImageView and UILabels arranged in stack views, the outermost of which I pinned to all four sides of the content view.
But when I run the app in the simulator, I do not see the image views. Debugging the view hierarchy, I see they are getting clipped.
And digging in a little further by focusing on the outermost stack view with "Show Constraints" on, I don't see my auto layout constraints at all (instead I get an ambiguous position runtime warning for each of the outermost stack views).
The text label that expands to the right is set up with 0 lines and the table view gets a constant estimatedRowHeight value and its rowHeight set to UITableViewAutomaticDimension, so I think if these constraints were working, the rows in the table would size to fit the content. Perhaps notably, any width or height auto layout constraint I add does appear when I debug the view hierarchy, so I know some constraints are present at runtime, I just cannot for the life of me figure out why the constraints relative to the content view are not.
After wrestling with this for a long time and having a much more experienced developer look over my project, it seems as if there was nothing wrong with the way it was set up. Recreating the table view cell from scratch with exactly the same layout and constraints and using the exact same code fixed this issue. I had messed around with the stack views and constraints quite a bit before this started happening, so it might be possible to get Xcode into a place where it does not properly set up constraints when you run the app in the simulator.
Try to solve all warnings which are printed into your console log when you run your application and viewing this screen. Because you can not ignore warnings always, many warnings you must have to remove by correcting or solving issues in your constraint. And that is the only solution which helps you most of the time.
I'm 5 years late to the party, but I managed to find a solution to this problem in my project. If any of the views that are contained in the table view cell have a bad subclass set on them, the intializer defaults them back to a regular UIView and without any of the layout constraints. Hopefully that saves someone the hours it took me!

Some iOS constraints not working in Xcode 8 (after conversion to Swift 3)

I have quite a lot of views that are created with storyboards. Everything except 2 items in the views are not working correctly and it has to do with the constraints. It is almost like the constraints simply are not working or something.
When I am in the storyboard mode and change the constant value on the constraints, then 'update frames' nothing happens. It doesn't shift around like it typically does when constraints change.
This is inside a table cell. I have a view that is .5 thick that I want to stick to the bottom, and then I want it to extend to the trailing edge and to the leading. When I set any of these constraints then press 'update frames', nothing happens.
The other control I'm having an issue with is a TextView. It seems to have no height when in the application so again, something is going on with the constraints. This looks to be the same thing happening in this case too.
I have tried recreating the view in Xcode 8 (just this one view that I'm having issues with inside my storyboard) and I get the same weird behavior. I also tried saving it as Xcode 7.x mode as suggested in another thread, and I've also tried setting viewNeedsLayout in the code. Neither of those are working.
Any suggestions?
I see this issue here too. As a workaround:
Drag the affected view to a slightly other position (in Storyboard).
Now the (->) shows up to signal the current position does not match the constraints:
Click the (->)and choose "fix misplacements":
So I've now figured out issues for both of my issues.
It was a view that was 0.5 height but it did not have a constraint to be at that height. I added a constraint, updated frames, and it updated to be correct as I would expect.
No clue how something like this could be fine in Xcode 7 then in Xcode 8 it works. Apple really doesn't make developing easy huh.
2.
The textview I was using had a height constraint set to <= 107. Well I changed it to = 107 and now it works fine. = actually works for me too since I'm changing the constant dynamically in the code anyways. However, again, I still have no clue why this would work fine in Xcode 7 and not Xcode 8.

iOS Swift, Constraint pushing view off sotryboard

I'm working on my first swift app and I found out quickly that I need to use constraints to properly layout my page. So far I'm running into two issue.
The first one is that after adding some constraints, my view is pushed off the screen on the storyboard, but it looks fine(outside of my second issue) in the Assistant editor.
My second issue is that on some phone sizes the constraints are causing the text fields to expand when viewed in the Assistant editor and on an iPhone 6s test device.
I've added some screen shots below. Any help with this issue would be really appreciated.
Each time you add a constraint in storyboard, it is not automatically applied to your working view. You may think it should be applied immediately, but try to think think like this. If you have a view, and you apply height constraint (for example) and if Xcode applied it automatically you would get a view of width 0, since you haven't applied width constraint. That's why you need to tell Xcode to update constraints explicitly.
So when you want to update constraints, select view you want to update and go to Resolve Auto Layout issues, and click on Update Frames. This is located in the bottom right corner of your storyboard. It looks like a small triangle between two lines. Here you can update all views or just selected views.
For the second issue, you need to apply fixed width constraint to your stack view, instead of adding leading and trailing constraints. By doing that stack view will have the same width on each device.

Why do I get an autolayout error on a constraint that should not be installed for the size class?

I'm using iOS 8. My nib has autolayout and size classes enabled. I've made two layouts in IB, one for Any Width/Any Height, and one for Any Width/Compact Height. I've tested different orientations and device sizes in IB and I don't have constraint problems in IB.
Now when I run the app on my phone. The view lays out correctly in portrait. Then I rotate my phone into landscape and I get a "Unable to simultaneously satisfy constraints." error. When I look at the list of conflicting constraints, I see a constraint that should have been uninstalled for landscape layouts. When rotation completes the landscape layout appears correctly.
It's as if iOS is trying to lay out the view with the new bounds before the old constraints are uninstalled. Is this a known problem with iOS? Is there a step I need to implement to correctly support size classes?
I ran into this same issue. Very frustrating! At first I added code to willTransitionToTraitCollection:withTransitionCoordinator: in order to detect if the newCollection was a size class that didn't need the constraint. If so, then I set the constraint.active = NO.
It did the trick, but I hated the code. I was just like, "Why should I have to do this? The constraint is not installed for the new size class!"
Then I figured out that if I lowered the priority of the offending constraint to 999 in interface builder I no longer got the constraint error in the console when I rotated. :-) And all is right with the world again.
I still think this is an iOS bug, but I don't have time to make a sample project to reproduce it. I think what is unusual about this layout is that I have an image view that moves to an entirely different location when the phone is rotated into landscape.
The width constraint on the image view had different constants for either size class, but this was throwing an exception.
The next thing tried was to use different width constraints. One was installed for compact height, the other was installed for everything else. This was still throwing an exception, even though the breaking constraint should not have been installed.
Finally, the solution I got working was to have two image views, one for regular layouts, and one for compact height. Only one image view was installed at a time. This works without exceptions and looks exactly the same.

Storyboard preview differs from simulator

I'm currently trying to grapple with constraints (using Xcode 6 GM seed). Using reset to suggested constraints, everything looks right in the universal storyboard and in the preview:
As you can see, I'm previewing for multiple screen sizes. All of them have the image of the dot (and the image itself takes up the whole view, set to draw in the center) in the center. However, when I load it on the simulator, I see:
I'm not sure why this is happening. As far as I can tell, the constraints are all as they should be:
Is there something I am doing? Or is there a bug here in xcode 6?
Thanks in advance!
You don't need to add Leading, trailing, and vertical space constraints. To place your dot in center you need to add Horizontal and Vertical center in container constraint. Place your dot in the center of controller and add these two like in the given image.
The storyboard "Preview" doesn't match the simulator if you have any constraint errors/conflicts. I was having this problem, and found this question while searching for an answer.
One I got all of the conflicts resolved, the Preview worked. It still seems like the preview should consistently show what the simulator would show to help during the setup.

Resources