diff --git a/PHDemo/PHDemo/AlertVC.swift b/PHDemo/PHDemo/AlertVC.swift index a27f5a6..a4d80df 100644 --- a/PHDemo/PHDemo/AlertVC.swift +++ b/PHDemo/PHDemo/AlertVC.swift @@ -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) } } diff --git a/Sources/ProHUD/Alert/AlertConvenienceLayout.swift b/Sources/ProHUD/Alert/AlertConvenienceLayout.swift index aaf5eec..6ff2804 100644 --- a/Sources/ProHUD/Alert/AlertConvenienceLayout.swift +++ b/Sources/ProHUD/Alert/AlertConvenienceLayout.swift @@ -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) } } } diff --git a/Sources/ProHUD/Alert/AlertDefaultLayout.swift b/Sources/ProHUD/Alert/AlertDefaultLayout.swift index e61d34b..69da7cc 100644 --- a/Sources/ProHUD/Alert/AlertDefaultLayout.swift +++ b/Sources/ProHUD/Alert/AlertDefaultLayout.swift @@ -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 diff --git a/Sources/ProHUD/Core/Models/Action.swift b/Sources/ProHUD/Core/Models/Action.swift index ebb1cc6..23775aa 100644 --- a/Sources/ProHUD/Core/Models/Action.swift +++ b/Sources/ProHUD/Core/Models/Action.swift @@ -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? diff --git a/Sources/ProHUD/Core/Views/Button.swift b/Sources/ProHUD/Core/Views/Button.swift index 030bbb1..a5cfa8a 100644 --- a/Sources/ProHUD/Core/Views/Button.swift +++ b/Sources/ProHUD/Core/Views/Button.swift @@ -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 } }