@@ -178,23 +178,35 @@ - (UIView *)_makeBackgroundViewWithBlur:(BOOL)withBlur TOROUNDEDBUTTON_OBJC_DIRE
178178- (void )layoutSubviews {
179179 [super layoutSubviews ];
180180
181+ // Determine the content view's available maximum size accounting for the insetting
181182 const CGSize boundsSize = self.bounds .size ;
182- _contentView. frame = (CGRect){
183+ const CGRect contentBounds = (CGRect){
183184 .origin .x = _contentInset.left ,
184185 .origin .y = _contentInset.top ,
185186 .size .width = boundsSize.width - (_contentInset.left + _contentInset.right ),
186187 .size .height = boundsSize.height - (_contentInset.top + _contentInset.bottom ),
187188 };
188189
189- // Configure the button text
190- if (_titleLabel) {
191- [_titleLabel sizeToFit ];
192- _titleLabel.center = (CGPoint){
193- .x = CGRectGetMidX (_contentView.bounds ),
194- .y = CGRectGetMidY (_contentView.bounds )
195- };
196- _titleLabel.frame = CGRectIntegral (_titleLabel.frame );
197- }
190+ // Let the content view shrink itself to wrap its content if needed,
191+ // and position it in the middle of the view.
192+ UIView *const contentView = _overrideContentView ?: _contentView;
193+ contentView.frame = ({
194+ CGRect frame = contentBounds;
195+ frame.size = [contentView sizeThatFits: contentBounds.size];
196+ frame.origin .x = (boundsSize.width - frame.size .width ) * 0 .5f ;
197+ frame.origin .y = (boundsSize.height - frame.size .height ) * 0 .5f ;
198+ CGRectIntegral (frame);
199+ });
200+
201+ // Lay out the title label
202+ if (!_titleLabel) { return ; }
203+
204+ [_titleLabel sizeToFit ];
205+ _titleLabel.center = (CGPoint){
206+ .x = CGRectGetMidX (_contentView.bounds ),
207+ .y = CGRectGetMidY (_contentView.bounds )
208+ };
209+ _titleLabel.frame = CGRectIntegral (_titleLabel.frame );
198210}
199211
200212- (void )sizeToFit { [super sizeToFit ]; }
@@ -413,7 +425,24 @@ - (void)setContentView:(UIView *)contentView {
413425 _titleLabel = nil ;
414426 [_contentView removeFromSuperview ];
415427 _contentView = contentView ?: [UIView new ];
416- [self addSubview: _contentView];
428+ [_containerView addSubview: _contentView];
429+ [self setNeedsLayout ];
430+ }
431+
432+ - (void )setOverrideContentView : (UIView *)overrideContentView {
433+ if (_overrideContentView == overrideContentView) { return ; }
434+ if (_overrideContentView != nil ) {
435+ [_overrideContentView removeFromSuperview ];
436+ }
437+
438+ _overrideContentView = overrideContentView;
439+
440+ if (_overrideContentView != nil ) {
441+ _contentView.hidden = YES ;
442+ [_containerView addSubview: _overrideContentView];
443+ } else {
444+ _contentView.hidden = NO ;
445+ }
417446 [self setNeedsLayout ];
418447}
419448
0 commit comments