假设我们有这个CustomButton
接口:
@interface CustomButton : UIButton
@property (nonatomic, assign) CGFloat minWidth;
@end
每次minWidth
改变,我们都要CustomButton
重新布局。据我所知,我们有两种解决方案:
观察财产的价值
// In -initWithFrame:
[self addObserver:self forKeyPath:@"minWidth" options:0 context:nil];
// In -observeValueForKeyPath:ofObject:change:context:
[self setNeedsLayout];
覆盖minWidth
的设置器
// In -setMinWidth:
_minWidth = minWidth; // Side note: it's an ARC project
[self setNeedsLayout];
哪一个是正确的解决方案,为什么?