Swift : how to check which view is causing constraints error - swift

I am getting the following error when trying to load a VC:
Thread 1: Exception: "Unable to activate constraint with anchors
<NSLayoutXAxisAnchor:0x280fea300 \"i6.I6FuelSheetTextField:0x1030fe400.centerX\"> and
<NSLayoutXAxisAnchor:0x280fea440
\"UILayoutGuide:0x282327800'UIViewSafeAreaLayoutGuide'.centerX\"> because they have no common
ancestor. Does the constraint or its anchors reference items in different view hierarchies?
That's illegal."
How can I identify which views these addresses are referring to?

This is occurring because you are trying to constraint two views that are unrelated to each other. You are lucky enough to get enough information from the error. You are using a component that an instance of I6FuelSheetTextField and you trying to constraint it's centerX attribute. So, I think the error itself is pretty explanatory.
Note: It could also be the case when you forget to add a view to another view's subview.

Related

nil error when attempting to set textfield placeholder, segment uiview

xcode v:9.2
I am trying to load user information from the database to set as placeholder text. I have tested this code on a simpler project that does not have segment views, and it works fine. I did some research and I think it might be a hierarchical issue. I am struggling to understand how I can make the textfield values load so that the locations they point to are connected when I try to access them and set placeholder values.
GetCurr retrieves database values and sends them to completion handler function to write placeholder value. When run, "setPH = nil"
Calls GetCurr for email
Completion handler that sets placeholder text
loadUserView should tell when to load the values of this particular segment
I know it's not my IBOutlets because they are all connected and as previously said, this code works perfectly fine in a simpler app that does not have segment views.
I figured it out! I connected the segments to uiviews that had their own uiviewcontroller class, as subclasses of the parent base class. It also makes my code cleaner and prettier!
Useful:
https://docs.swift.org/swift-book/LanguageGuide/Inheritance.html
https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/

SceneKit crashes when I use constraint and minimize App

I have very strange behavior in my app, that I can't explain. I have a SceneKit session running, and some nodes have constraints assigned (no matter what kind). When app goes to background I'm removing all nodes from the scene, and when it appears again I'm adding them again (This is needed for reasons not needed here). When I have this constraints assigned and resume app - it crashes. Not always, but very often. I have no idea what to do to fix this. I tried removing constraints when removing nodes, but this did not help. The error I get is in a screenshot. Can someone help me find a solution?
Your question doesn't show the code that you are referencing, but I can tell you that an EXC_BAD_ACCESS error typically means you are trying to access an object that has been released from memory.
Without going in depth about strong vs. weak variables, you likely have a weak or unowned variable (to that constraint) that is becoming nil and that you are trying to access (by force-unwrapping).
When you are removing these nodes, or when the view disappears, the constraint is released from memory. So the fix here is to either make the reference to this constraint strong or to make a new constraint, keep a strong reference to that in the class, and assign that constraint to the object.
Assuming your reference is to a storyboard, replace:
#IBOutlet weak var constraint: NSLayoutConstraint!
With:
#IBOutlet var constraint: NSLayoutConstraint!
Also note that the variable is probable force-unwrapped (NSLayoutConstraint!), which is likely why you are running into the EXC_BAD_ACCESS error. Set your constraint to be a strong variable instead of weak, or better yet, avoid force-unwrapping optionals if possible!
Note: If this doesn't solve your problem, leave a comment after adding more details to your question.

Confused by conflicting autolayout constraints

I got this crashlog when I click on tabbar tabitem to change viewcontroller:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0xdb98e30 h=--- v=--- H:[UIWindow:0xc65c530(320)]>",
"<NSAutoresizingMaskLayoutConstraint:0xdbba9a0 h=-&- v=-&- UITransitionView:0xdb43ab0.width == UILayoutContainerView:0xdb42ef0.width>",
"<NSAutoresizingMaskLayoutConstraint:0xc680b80 h=-&- v=-&- UILayoutContainerView:0xdb42ef0.width == UIWindow:0xc65c530.width>",
"<NSAutoresizingMaskLayoutConstraint:0x119352f0 h=-&- v=-&- UIViewControllerWrapperView:0xc68a2c0.width == UITransitionView:0xdb43ab0.width>"
)
Will attempt to recover by breaking constraint
<NSAutoresizingMaskLayoutConstraint:0x119352f0 h=-&- v=-&- UIViewControllerWrapperView:0xc68a2c0.width == UITransitionView:0xdb43ab0.width>
Break on objc_exception_throw to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
First, how are you applying these constraints? The most common cause of these conflict messages is adding constraints without ensuring they are the only ones the view has. Have you set the property
translatesAutoresizingMaskIntoConstraints = NO
before trying to set more? Because you are getting this exception at runtime it looks like Xcode has accepted what you are trying to do so you are probably adding constraints in code.
Did you add all of those constraints in the list that are given as conflicting? If so, remove them and replace them one by one to find which one you got wrong.
If you are doing all this in Xcode in what we used to know of as Interface Builder, use the new tools to examine all of the constraints and verify that you have not added constraints that conflict. Xcode can help you here.
If you are adding constraints in code but you have created the view in IB then you probably want to either manage all constraints in IB or you want to set the xib to NOT use autolayout, so that it does not apply constraints that conflict with what you later add.

Why is this autolayout specification not sufficient?

I am trying to get rid of those annoying warnings in Interface Builder, but I do not understand what it is complaining about (all Interface Builder, no code):
I have specified a fixed with, fixed height and fixed distances to right and top.
Yet the warning tells me
Needs Constraints for: Y position, height
Needs Constraints for: X position, width
Can someone please explain how these are constraints are not sufficient?
Edit
Also, when using the "automatic" add constraints commands, it does nothing and the errors remain.
I would suspect the superview is not pinned to a size in interface builder. Autolayout constraints seem to solve everything simultaneously and an error message that looks like it relates to one view can just as easily relate to the other view in the relationship.
What is - (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis telling you?

"Unable to simultaneously satisfy constraints" error with UIScrollView

Created a simple ViewController, and added UIScrollView with a xib. When I'm trying to change the height of the scroll view, I'm getting a weird exception:
Unable to simultaneously satisfy constraints. Probably at least one
of the constraints in the following list is one you don't want. Try
this: (1) look at each constraint and try to figure out which you
don't expect; (2) find the code that added the unwanted constraint or
constraints and fix it. (Note: If you're seeing
NSAutoresizingMaskLayoutConstraints that you don't understand, refer
to the documentation for the UIView property
translatesAutoresizingMaskIntoConstraints) (
"",
"",
"",
"" )
Will attempt to recover by breaking constraint
(Names: '|':UIView:0xa4ab830 )>
Break on objc_exception_throw to catch this in the debugger. The
methods in the UIConstraintBasedLayoutDebugging category on UIView
listed in may also be helpful.
Here's how my xib looks:
Found on the SO that setting content size and setting translatesAutoresizingMaskIntoConstraints property to YES might help. Tried it, didn't work:
topScrollView.translatesAutoresizingMaskIntoConstraints = YES;
topScrollView.contentSize = CGSizeMake(1000, 2000);
EDIT: added screenshot