Created
September 3, 2013 09:26
-
-
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}?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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