支持plain样式按钮

This commit is contained in:
xaoxuu 2023-03-23 11:31:23 +08:00
parent 238ffa1055
commit 2f6b514666
5 changed files with 47 additions and 10 deletions

View File

@ -69,6 +69,24 @@ class AlertVC: ListVC {
}
}
list.add(title: "文字 + 按钮") { section in
section.add(title: "只有一段文字 + 无背景色按钮") {
Alert { alert in
alert.config.boldTextFont = .systemFont(ofSize: 15)
alert.config.buttonFont = .systemFont(ofSize: 15)
alert.config.cardCornerRadius = 12
alert.vm.title = "为了维护社区氛围,上麦用户需进行主播认证"
let v = UIView()
v.backgroundColor = UIColor("#F7F7F7")
alert.add(subview: v).snp.makeConstraints { make in
make.left.right.equalToSuperview()
make.height.equalTo(1)
}
alert.add(spacing: 16)
alert.add(action: "取消", style: .plain(textColor: UIColor("#939999")))
alert.add(action: "确定", style: .plain(textColor: UIColor("#14cccc")))
}
}
section.add(title: "只有一段文字 + 按钮") {
Alert { alert in
alert.vm.title = "只有一段文字"
@ -247,13 +265,13 @@ class AlertVC: ListVC {
imgv.contentMode = .scaleAspectFill
imgv.clipsToBounds = true
imgv.layer.cornerRadiusWithContinuous = 12
alert.add(subview: imgv).snp.makeConstraints { make in
alert.add(action: imgv).snp.makeConstraints { make in
make.height.equalTo(120)
}
// seg
let seg = UISegmentedControl(items: ["开发", "测试", "预发", "生产"])
seg.selectedSegmentIndex = 0
alert.add(subview: seg).snp.makeConstraints { make in
alert.add(action: seg).snp.makeConstraints { make in
make.height.equalTo(40)
make.width.equalTo(400)
}
@ -262,8 +280,8 @@ class AlertVC: ListVC {
slider.minimumValue = 0
slider.maximumValue = 100
slider.value = 50
alert.add(subview: slider)
alert.add(spacing: 24)
alert.add(action: slider)
alert.add(spacing: 24, for: alert.actionStack)
alert.add(action: "取消", style: .gray)
}
}
@ -275,7 +293,7 @@ class AlertVC: ListVC {
s1.minimumValue = 0
s1.maximumValue = 40
s1.value = Float(alert.config.cardCornerRadius ?? 16)
alert.add(subview: s1).snp.makeConstraints { make in
alert.add(action: s1).snp.makeConstraints { make in
make.height.equalTo(50)
}
if #available(iOS 14.0, *) {
@ -287,7 +305,7 @@ class AlertVC: ListVC {
// Fallback on earlier versions
}
alert.config.actionAxis = .vertical
alert.add(spacing: 24)
alert.add(spacing: 24, for: alert.actionStack)
alert.add(action: "OK", style: .gray)
}
}

View File

@ -89,17 +89,30 @@ extension Alert: InternalConvenienceLayout {
// MARK:
@discardableResult public func add(subview: UIView) -> UIView {
contentStack.addArrangedSubview(subview)
if #available(iOS 11.0, *) {
if let last = contentStack.arrangedSubviews.last {
contentStack.setCustomSpacing(0, after: last)
}
}
return subview
}
@discardableResult public func add(action subview: UIView) -> UIView {
actionStack.addArrangedSubview(subview)
return subview
}
// MARK:
public func add(spacing: CGFloat) {
add(spacing: spacing, for: contentStack)
}
public func add(spacing: CGFloat, for stack: UIStackView) {
if #available(iOS 11.0, *) {
if let last = actionStack.arrangedSubviews.last {
actionStack.setCustomSpacing(spacing, after: last)
if let last = stack.arrangedSubviews.last {
stack.setCustomSpacing(spacing, after: last)
}
}
}

View File

@ -106,7 +106,7 @@ extension Alert: DefaultLayout {
func setDefaultAxis() {
guard isViewDisplayed == false && config.actionAxis == nil else { return }
let count = actionStack.arrangedSubviews.count
let count = actionStack.arrangedSubviews.filter({ $0.isKind(of: UIControl.self )}).count
guard count < 4 else { return }
if isPortrait && count < 3 || !isPortrait {
actionStack.axis = .horizontal

View File

@ -28,6 +28,7 @@ open class Action: NSObject {
case destructive // red button, background with systemRed
case filled(color: UIColor) // background with custom color
case light(color: UIColor) // light background with custom color, text with custom color
case plain(textColor: UIColor) // plain text
}
public private(set) var identifier: String?

View File

@ -56,6 +56,11 @@ open class Button: UIButton {
case .light(let color):
setTitleColor(color, for: .normal)
backgroundColor = color.withAlphaComponent(0.15)
case .plain(let textColor):
setTitleColor(textColor, for: .normal)
backgroundColor = .none
contentEdgeInsets.top = 0
contentEdgeInsets.bottom = 4
}
}