How to add constraints after addSubview so subview resizes using supervisor

I have an example project that demonstrates the problem here

https://github.com/ericgorr/autolayout_with_addsubview.git

I have a view called CalcView that I want to programmatically add as a subroutine to the view in the main application window. When I resize the window, I want CalcView to be resized.

In windowDidLoad in MainWindowController, I add a subview by doing:

let calcViewController = ELIZCalcView() let calcView = calcViewController.view calcContentView?.addSubview( calcViewController.view ) 

I am trying to add restrictions by doing:

 let bindings = [ "calcView": calcView ] let horizontalContraint:[AnyObject] = NSLayoutConstraint.constraintsWithVisualFormat( "H:|[calcView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: bindings ) let verticalContraint:[AnyObject] = NSLayoutConstraint.constraintsWithVisualFormat( "V:|[calcView]|", options: NSLayoutFormatOptions(0), metrics: nil, views: bindings ) calcContentView?.addConstraints( horizontalContraint ) calcContentView?.addConstraints( verticalContraint ) 

Now, for those who know how to correctly interpret this code, it is likely that this will not work. After starting my application, I cannot resize the window at all. In addition, I see the following error message in the console:

 2015-07-04 16:04:45.019 aocsCalc[5797:3526462] Unable to simultaneously satisfy constraints: ( "<NSLayoutConstraint:0x618000082440 V:|-(0)-[NSView:0x600000120f00] (Names: '|':aocsCalc.ELIZHighlightView:0x608000120500 )>", "<NSAutoresizingMaskLayoutConstraint:0x618000084100 h=--& v=&-- V:|-(-2)-[NSView:0x600000120f00] (Names: '|':aocsCalc.ELIZHighlightView:0x608000120500 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x618000082440 V:|-(0)-[NSView:0x600000120f00] (Names: '|':aocsCalc.ELIZHighlightView:0x608000120500 )> Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. And/or, break on objc_exception_throw to catch this in the debugger. 

If I remove the vertical restriction, the error message will disappear and I can resize the window vertically.

So, what do I need to do so that CalcView changes with the window?

+7
xcode autolayout cocoa swift macos
source share
1 answer

Before adding CalcView as a subquery, try pasting this line of code:

 calcView.translatesAutoresizingMaskIntoConstraints = false 

This should solve your problem.

By default in UIView / NSView this property is set to YES / true and creates its own set of restrictions based on the autoresist mask. These automatic restrictions conflict with those you created in the code.

This clearly indicates this in the error description. Lines 2 and 3 show that there are 2 sets of vertical limits for one view - NSView:0x600000120f00 , which seems to be your calcView.

 <NSLayoutConstraint:0x618000082440 V:|-(0)-[NSView:0x600000120f00] <NSAutoresizingMaskLayoutConstraint:0x618000084100 h=--& v=&-- V:|-(-2)-[NSView:0x600000120f00] 

They are both vertical. First you need to click the top view from above without borders. The second (created automatically) wants to bind it with a small margin, presumably taken from the way it is laid out in Interface Builder.

UPDATE

Create a new Cocoa application project and paste the following code:

 override func viewDidLoad() { super.viewDidLoad() let view = NSView() view.translatesAutoresizingMaskIntoConstraints = false self.view.addSubview(view) //Making it red just to see a little better. Ignore this two lines. view.wantsLayer = true view.layer?.backgroundColor = CGColorCreateGenericRGB(1, 0, 0, 1) //----------------------------------------------------------------- let views = ["view" : view] self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("|-[view]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: views)) self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-[view]-|", options: nil, metrics: nil, views: views)) } 

The newly created NSView is attached to the main view of View Controllers with standard fields (8 points, which are indicated as "-" in the line of the visual format) and resizes from the main view (see the figures). A little tip - you do not need to specify "H:" in visual format, only "V:". Horizontal by default.

This should give you an idea of ​​how programmatic actions are adding constraints. The code may not be optimal, I have code in Obj-C and I know very little Swift.

I uploaded your project. The error is probably somewhere in your complex hierarchy of xib views and manipulations. But this is a completely different story. Also be careful with scroll views, they are a bit complicated when it comes to self-timer, you can find many possibilities for this on SO. Happy coding :)

enter image description hereenter image description here

+12
source share

All Articles