Skip to content

Instantly share code, notes, and snippets.

@odrobnik
Created September 3, 2013 09:26
Show Gist options
  • Save odrobnik/6421605 to your computer and use it in GitHub Desktop.
Save odrobnik/6421605 to your computer and use it in GitHub Desktop.
Why does setting the diameter cause the size to go to {0,0}?
@implementation ExpandButton
{
NSLayoutConstraint *_widthConstraint;
}
- (id)initWithFrame:(CGRect)frame
{
CGFloat smallerDim = MIN(frame.size.height, frame.size.width);
self = [super initWithFrame:frame];
if (self)
{
// stop autoresizing mask from interfering
self.translatesAutoresizingMaskIntoConstraints = NO;
// width and height should always be equal
NSLayoutConstraint *aspectConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
[self addConstraint:aspectConstraint];
// specify a constraint for the width
_widthConstraint = [NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:smallerDim];
[self addConstraint:_widthConstraint];
}
return self;
}
- (void)setDiameter:(CGFloat)diameter
{
if (_diameter == diameter)
{
return;
}
_widthConstraint.constant = _diameter;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment