After a custom segue and a custom presentation animator, I set up constraints of the presented view.
let views = ["view" : childVC.view]
let horizontalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: [], metrics: nil, views: views)
let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: [], metrics: nil, views: views)
parentVC.view.addConstraints(horizontalConstraints)
parentVC.view.addConstraints(verticalConstraints)
When I print out the constraints of the parent/presenting viewcontroller's view, there are only these four constraints. Now I expect my subview to be as resizable as the NSWindow, but both aren't resizable anymore.
I also give the translatesAutoresizingMaskIntoConstraint = false
a try, but I do not really understand it. Furthermore, it removes everything from the views.
So what I'm missing here?
It sounds like your view is not using auto layout internally. It is relying on the old springs-and-struts model. That's why when you turn off translatesAutoresizingMaskIntoConstraint
it "removes everything". More likely, the view is collapsing to zero size because there are no constraints to hold its size and position anymore.
When translatesAutoresizingMaskIntoConstraint
is on, constraints are generated to hold the view at the last frame that was explicitly set for it. If its superview resizes and if the superview autoresizes its subviews (which is the default), the view will get resized according to its autoresizingMask
. But auto layout sort of doesn't know that will happen. The generated constraints keep the frame like it is until the superview changes.
But you've also added constraints between the window's content view and your view. Those constraints effectively prevent the window from resizing because the view's constraints don't allow the layout system to resize it. Only explicit changes to its frame will do that. You're sort of in a catch-22. The view would resize if its superview resized, but the superview can't resize because the view has inflexible constraints on it.
In general, when you're working with a view which has translatesAutoresizingMaskIntoConstraint
left on, you are very restricted in what other constraints you can usefully apply to that view. You can't really use constraints that "push" on the view's frame or size. You can only loosely "hang" other views off of that view.
In any case, you need to adopt auto layout for your view or just don't set constraints between it and its superview. Instead rely on the autoresizing mechanism.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments