Skip to content

Commit 0108fe5

Browse files
committed
Add an override content view property for temporary views
1 parent 237ff64 commit 0108fe5

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

TORoundedButton/TORoundedButton.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
4949
/// this property to your own custom UIView subclass in order to more efficiently manage sizing and layout.
5050
@property (nonatomic, strong, null_resettable) UIView *contentView;
5151

52+
/// An additional content view that will hide `contentView` and show in its place when set.
53+
/// When this is set back to `nil`, the original content view is restored.
54+
/// This is useful for temporarily showing an alternative UI, such as a loading or download spinner.
55+
@property (nonatomic, strong, nullable) UIView *overrideContentView;
56+
5257
/// The amount of inset padding between the content view and the edges of the button.
5358
/// (Default value is 15 points inset from each edge).
5459
@property (nonatomic, assign) UIEdgeInsets contentInset;
@@ -60,6 +65,7 @@ IB_DESIGNABLE @interface TORoundedButton : UIControl
6065
@property (nonatomic, assign) UIBlurEffectStyle blurStyle;
6166

6267
/// The text that is displayed in center of the button (Default is nil).
68+
/// This adds an internally controlled label view to the main content view.
6369
@property (nonatomic, copy, nullable) IBInspectable NSString *text;
6470

6571
/// The attributed string used in the label of this button.

TORoundedButton/TORoundedButton.m

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)