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?
Control + Drag from the view to itself, then set the aspect ratio to 1: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]];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With