I am trying to create a UIView (A) containing 2 user views (B) in it
View B is installed using Autolayout restrictions and runs in Interface Builder, including restrictions. A is added to the Nib viewController
B - UIImageView (Leading = 10, Trailing = 10, AlignVertically) - UITextField (Leading = 10, Trailing = 10, AlignVertically)
ViewController A (300x300, AlignHorizontally, AlignVertically)
In ViewController, I have A that should be set to 300x300 and B1 and B2 have their own leaders, trailing, top and bottom fixed to 0. (which should make B1 and B2 equal to 300x150, forgive me if I miss something )
When loading View B, I use the following code to load my Nib:
override func awakeAfterUsingCoder(aDecoder: NSCoder!) -> AnyObject! { if self.subviews.count == 0 { let bundle = NSBundle(forClass: self.dynamicType) var view = bundle.loadNibNamed("B", owner: nil, options: nil)[0] as B view.setTranslatesAutoresizingMaskIntoConstraints(false) let constraints = self.constraints() self.removeConstraints(constraints) view.addConstraints(constraints) return view } return self }
But when I try to run this, I get the following warning, including a failure:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7f897ad1acc0 V:[TestProject.B:0x7f897af73840(300)]> When added to a view, the constraint items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
I also tried to add the view as a property of View B and use the following code to add it to B
NSBundle.mainBundle().loadNibNamed("B", owner: self, options: nil) self.addSubview(self.viewOfB);
the result is a view added to the viewController, but it does not accept any of the AutoLayoutConstraints from its own Nib.
Right now I have no idea how to add this view to the viewController view, including the limitations. What am I doing wrong? Is there a better way to do this?
PS: View A is also used as a custom.
PPS: I use Swift to do this, but I'm sure the solutions in Objective-C also work.