弹框支持显示一个按钮

This commit is contained in:
fengshuo
2022-03-14 19:08:54 +08:00
parent 8c26f46776
commit 5633fa96fe
3 changed files with 43 additions and 16 deletions

View File

@@ -13,8 +13,16 @@
NS_ASSUME_NONNULL_BEGIN
@interface TTAlertConfig : NSObject
typedef NS_ENUM(NSUInteger, TTAlertActionStyle) {
TTAlertActionConfirmStyle = 0, // 只有确定按钮
TTAlertActionCancelStyle = 1, // 只有取消按钮
TTAlertActionBothStyle = 2, // 全部按钮
};
@interface TTAlertConfig : NSObject
// 按钮显示风格
@property (nonatomic, assign) TTAlertActionStyle actionStyle;
// 容器背景设置
@property (nonatomic, strong) UIColor *backgroundColor;

View File

@@ -30,6 +30,8 @@ static CGFloat kAlertButtonCornerRadius = 8.f;
kAlertTitleFont = 16.f;
kAlertCornerRadius = 14.f;
kAlertButtonCornerRadius = 19.f;
_actionStyle = TTAlertActionBothStyle;
// title
_title = @"";//

View File

@@ -21,6 +21,7 @@ static CGFloat const kBtnHeight = 38.f;
@property (nonatomic, strong) UILabel *messageLabel; //
@property (nonatomic, strong) UIButton *cancelButton; //
@property (nonatomic, strong) UIButton *confirmButton; //
@property (nonatomic, strong) UIStackView *stackView;
@end
@@ -39,8 +40,9 @@ static CGFloat const kBtnHeight = 38.f;
- (void)initViews {
[self addSubview:self.titleLabel];
[self addSubview:self.messageLabel];
[self addSubview:self.cancelButton];
[self addSubview:self.confirmButton];
[self addSubview:self.stackView];
[self.stackView addSubview:self.cancelButton];
[self.stackView addSubview:self.confirmButton];
}
- (void)initConstraints {
@@ -55,19 +57,20 @@ static CGFloat const kBtnHeight = 38.f;
make.left.right.mas_equalTo(self).inset(kPadding);
}];
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kBtnHeight);
make.right.mas_equalTo(self.mas_centerX).offset(-8);
make.left.mas_equalTo(kPadding);
make.bottom.mas_equalTo(-kPadding);
}];
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kBtnHeight);
make.left.mas_equalTo(self.mas_centerX).offset(8);
make.right.mas_equalTo(-kPadding);
make.bottom.mas_equalTo(-kPadding);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(-kPadding);
make.centerX.mas_equalTo(self);
}];
[self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kBtnHeight);
make.width.mas_equalTo(self.mas_width).multipliedBy(0.4);
}];
[self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kBtnHeight);
make.width.mas_equalTo(self.mas_width).multipliedBy(0.4);
}];
}
#pragma mark - Button Events
@@ -138,6 +141,9 @@ static CGFloat const kBtnHeight = 38.f;
_titleLabel.textColor = config.titleColor;
_titleLabel.font = config.titleFont;
self.cancelButton.hidden = config.actionStyle == TTAlertActionConfirmStyle;
self.confirmButton.hidden = config.actionStyle == TTAlertActionCancelStyle;
// cancel button
[_cancelButton setTitle:config.cancelButtonConfig.title forState:UIControlStateNormal];
[_cancelButton setTitleColor:config.cancelButtonConfig.titleColor forState:UIControlStateNormal];
@@ -227,4 +233,15 @@ static CGFloat const kBtnHeight = 38.f;
return _confirmButton;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.cancelButton, self.confirmButton]];
_stackView.distribution = UIStackViewDistributionFillEqually;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 16;
}
return _stackView;
}
@end