CustomButtonこのインターフェースがあるとしましょう:
@interface CustomButton : UIButton
@property (nonatomic, assign) CGFloat minWidth;
@end
変更のたびに、もう一度minWidthレイアウトしたいと思いますCustomButton。私の知る限り、解決策は 2 つあります。
資産価値の観察
// 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];
適切な解決策はどれですか?その理由は?