iOS AutoLayout Problems - Subviews Misplaced Even Though No Constraints - ios

I have a project where some of my views use constraints and some of them don't really need to because they show up nicely on other device sizes as well. My problem is that when I deleted all my constraints in a view controller's subviews, The subviews get heavily misplaced even though I didn't get any warnings about missing constraints.
Also, when I try to "reset all my subviews to suggested constraints", this adds no constraints whatsoever, and I'm stuck with misplaced views.
What could be the cause of this? Constraints are very tricky to work with.

Because,AutoLayout in XCode is default enabled.
Even you do not create any your constraints, XCode will auto create some constraints at build time so that iOS will know how to render the views
If you want to disable autoLayout,just uncheck the Use auto layout as image shown below

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!

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.

Views disappear when `translatesAutoresizingMaskIntoConstraints` set `NO`

Problem
Some third party library is used. Some views disappear after their translatesAutoresizingMaskIntoConstraints are set to NO.
Don't have other autoresizingMask setting for my views in my own code; in the library, the autoresizingMask parts of code have been removed too. Instead, explicit bounds/center/frame are set for those views. There're no nib files, views are all programmatically created.
I know some other people solve similar problem by giving a thorough autolayout constraints set, but in my case, I mean to turn off autolayout and do it manually. No idea when autolayout is turned on.
Some people say that "by default, as your app launches, autolayout is switched off, and the system behaves as in iOS 5 and before. But if, at any time while your app runs, the system sees an autolayout constraint (generated in code or by the loading of a nib that has “Use autolayout” checked), the autolayout system is switched on, and from then on you’re running under autolayout." (Programming iOS 6 by Matt Neuberg, pages 383-384), but in my project I don't think there's any autolayout constraints left.
The code is bulky, but will upload some skeleton if necessary.
Thank you for tips!
If you don't want to use Auto Layout you have to set translatesAutoresizingMaskIntoConstraints to YES. Here's a reference:
This works through the property
translatesAutoresizingMaskIntoConstraints. When this property is YES,
which it is by default, the autoresizing mask of a view is translated
into constraints. For example, if a view is configured as in Figure
6-1 and translatesAutoresizingMaskIntoConstraints is YES, then the
constraints |-20-[button]-20-| and V:|-20-[button(20)] are added to
the view’s superview. The net effect is that unaware views behave as
they did in versions of OS X prior to 10.7.
For views that are aware of Auto Layout, in most circumstances you
will want translatesAutoresizingMaskIntoConstraints to be NO. This is
because the constraints generated by translating the autoresizing mask
are already sufficient to completely specify the frame of a view given
its superview’s frame, which is generally too much. For example, this
will prevent a button from automatically assuming its optimal width
when its title is changed.

Suppress Autolayout Warnings

I have 3 image views that I have placed in the nib. They need to start at a different y value based on portrait or landscape. Additionally, they will move to the bottom of the screen based on a button click. At that point, I remove the top constraint and add a bottom constraint.
Due to the complexity of this, I decided to handle those particular constraints in the code. However, I still get a warning that the y position for those image views is ambiguous.
Is there any way for me to tell Xcode not to worry, that I have set the constraints in code? The only solution I can think of is to create a constraint in the nib and delete it on view did load but that seems hacky.
You can add constraints that are removed at build time. This tells interface builder that you're going to handle it in code. Working on capturing a screen shot.

Resources