The "width is equal to height" constraint in the interface builder for the same view: how to create such a constraint?

I want to create a "width equals height" constraint for the same view (so that my view will be square). The method given in this answer does not work, as it is not a restriction between two different views.

Is it possible?

+5
source share
2 answers

Control + Drag from view to yourself, then set aspect ratio to 1: 1.

+13
source

Configure the window in Interface Builder to contain the NSBox and set limits to the default value from all sides. Then add {IBOutlet NSBox *box;} to AppDelegate.h , and in IB connect the outlet to the box. In AppDelegate.m add the following to applicationDidFinishLaunching and run the code. I think this is what you need. If you add your restrictions programmatically, be sure to add enough restrictions on height and width to indicate what you want. Just add this restriction in addition to other restrictions.

 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { box.translatesAutoresizingMaskIntoConstraints = NO; [box addConstraint: [NSLayoutConstraint constraintWithItem:box attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:box attribute:NSLayoutAttributeHeight multiplier:1 constant:0]]; } 
+1
source

Source: https://habr.com/ru/post/1205490/


All Articles