8

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?

Community
  • 1
  • 1
Colas
  • 3,473
  • 4
  • 29
  • 68

2 Answers2

16

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

gabbler
  • 13,626
  • 4
  • 32
  • 44
1

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]];
}
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
rbkopelman
  • 39
  • 3