Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint "width equals height" in interface builder, for the same view: how to create such a constraint?

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

Is it possible?

like image 284
Colas Avatar asked Oct 25 '14 09:10

Colas


2 Answers

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

like image 118
gabbler Avatar answered Oct 19 '22 19:10

gabbler


Set up a window in Interface Builder to contain an NSBox & set constraints to standard value on all sides. Then add {IBOutlet NSBox *box;} to AppDelegate.h & in IB connect the box outlet to your box. In AppDelegate.m add the following to applicationDidFinishLaunching & run the code. I think this is what you are after. If you are adding your constraints programmatically, be sure to add enough height & width constraints to specify what you want. Just add this sort of constraint in addition to your other constraints.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    box.translatesAutoresizingMaskIntoConstraints = NO;
    [box addConstraint:
     [NSLayoutConstraint constraintWithItem:box
                                  attribute:NSLayoutAttributeWidth
                                  relatedBy:NSLayoutRelationEqual
                                     toItem:box
                                  attribute:NSLayoutAttributeHeight
                                 multiplier:1
                                   constant:0]];
}
like image 41
rbkopelman Avatar answered Oct 19 '22 21:10

rbkopelman