Skip to content

Commit

Permalink
fix:
Browse files Browse the repository at this point in the history
- 调整 text / title 视图的约束级别 (priority )
- text / title 默认 height, 修改为 greaterThanOrEqual, 约束级别调整为 defaultHigh(750)
- EmptyPageForText / text / title, 水平/垂直 - 抗压缩/抗拉伸 约束级别调整为 defaultHigh(749)
  • Loading branch information
linhay committed Mar 19, 2021
1 parent be4312a commit ca99ca0
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 61 deletions.
2 changes: 1 addition & 1 deletion EmptyPage.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'EmptyPage'
s.version = '4.0.3'
s.version = '4.0.4'
s.summary = 'iOS - 轻量级空白页占位图框架...'

s.homepage = 'https://github.com/linhay/EmptyPage'
Expand Down
3 changes: 1 addition & 2 deletions Example/EmptyPage/EmptyStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum TemplateSet {
font: UIFont.boldSystemFont(ofSize: 18))
})
.config(textLabel: {
$0.set(text: "Following people helps you keep what they're saying and recommending.",
$0.set(text: "Following people helps you \n keep what they're saying and recommending.",
color: UIColor("#c3ccd1"))
})
.config(button: {
Expand All @@ -109,7 +109,6 @@ enum TemplateSet {
.layout(view: .button, types: .height(30))
.layout(view: .textLabel, types: .afterSpac(10))
.layout(view: .titleLabel, types: .afterSpac(10))
.layout(view: .textLabel, types: .height(100))
}

static func image_normal() -> EmptyPageTemplateProtocol {
Expand Down
16 changes: 8 additions & 8 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
PODS:
- EmptyPage (4.0.2):
- EmptyPage/core (= 4.0.2)
- EmptyPage/managers (= 4.0.2)
- EmptyPage/templates (= 4.0.2)
- EmptyPage/core (4.0.2)
- EmptyPage/managers (4.0.2):
- EmptyPage (4.0.3):
- EmptyPage/core (= 4.0.3)
- EmptyPage/managers (= 4.0.3)
- EmptyPage/templates (= 4.0.3)
- EmptyPage/core (4.0.3)
- EmptyPage/managers (4.0.3):
- EmptyPage/core
- EmptyPage/templates (4.0.2):
- EmptyPage/templates (4.0.3):
- EmptyPage/core
- LookinServer (1.0.0)
- MJRefresh (3.4.3)
Expand Down Expand Up @@ -52,7 +52,7 @@ EXTERNAL SOURCES:
:path: "../EmptyPage.podspec"

SPEC CHECKSUMS:
EmptyPage: c9cfc17563ba994e963fabf44d968ac51e2cfa8c
EmptyPage: 39e865ba8e43aa8b5bdedb5c1a7f86910db46f38
LookinServer: 0002891cd3cec5e082a910e897dfe71b4f06b89e
MJRefresh: 53e3e3219f204425ee6d3e62e8733d3295944cd6
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
Expand Down
107 changes: 61 additions & 46 deletions Sources/Standard/EmptyPageForStandard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import UIKit

/// `EmptyPageForStandard` 混合样式模板
open class EmptyPageForStandard: UIStackView, EmptyPageTemplateProtocol {

public var edge: UIEdgeInsets = .zero

// MARK: - Public property
Expand All @@ -36,7 +36,7 @@ open class EmptyPageForStandard: UIStackView, EmptyPageTemplateProtocol {
public let textLabel = EmptyPageForText(frame: .zero)
/// 底部按钮 button
public let button = EmptyPageForButton(frame: .zero)

private var layoutCache = [ViewType: [LayoutType: [NSLayoutConstraint]]]()
private var views: [UIView] { return [imageView, titleLabel, textLabel, button] }

Expand All @@ -56,26 +56,26 @@ open class EmptyPageForStandard: UIStackView, EmptyPageTemplateProtocol {
axis = .vertical
distribution = .fill
alignment = .center

views.forEach { view in
view.isHidden = true
view.translatesAutoresizingMaskIntoConstraints = false
addArrangedSubview(view)
}

titleLabel.config { view in
view.font = UIFont.systemFont(ofSize: 16)
view.textColor = UIColor(red: 51 / 255, green: 51 / 255, blue: 51 / 255, alpha: 1)
}

textLabel.config { view in
view.font = UIFont.systemFont(ofSize: 14)
view.textColor = UIColor(red: 153 / 255, green: 153 / 255, blue: 153 / 255, alpha: 1)
}

_ = layout(type: .afterSpac(20), views: [.imageView])
_ = layout(view: .textLabel, types: [.afterSpac(28), .height(20)])
_ = layout(view: .titleLabel, types: [.afterSpac(8), .height(22.5)])
_ = layout(view: .textLabel, types: [.afterSpac(28), .heightGreaterThanOrEqual(20)])
_ = layout(view: .titleLabel, types: [.afterSpac(8), .heightGreaterThanOrEqual(22.5)])
_ = layout(view: .button, types: [.height(36)])
}

Expand Down Expand Up @@ -122,21 +122,25 @@ public extension EmptyPageForStandard {
enum LayoutType: RawRepresentable, Hashable {

public typealias RawValue = Int
case width(_: CGFloat)
case height(_: CGFloat)
case insets(_: CGFloat)
case afterSpac(_: CGFloat)
case width(CGFloat)
case height(CGFloat)
case insets(CGFloat)
case afterSpac(CGFloat)
case widthGreaterThanOrEqual(CGFloat)
case heightGreaterThanOrEqual(CGFloat)

public init?(rawValue: Int) {
return nil
}

public var rawValue: Int {
switch self {
case .width: return 0
case .height: return 1
case .insets: return 2
case .afterSpac: return 3
case .widthGreaterThanOrEqual: return 4
case .heightGreaterThanOrEqual: return 5
}
}
}
Expand All @@ -149,67 +153,78 @@ public extension EmptyPageForStandard {
case .button: return button
}
}

func layout(type: LayoutType, views: [ViewType]) -> Self {
views.forEach({ self.layout($0, type: type) })
return self
}

func layout(view: ViewType, types: [LayoutType]) -> Self {
types.forEach({ self.layout(view, type: $0) })
return self
}

func layout(type: LayoutType, views: ViewType...) -> Self {
return layout(type: type, views: views)
}

func layout(view: ViewType, types: LayoutType...) -> Self {
return layout(view: view, types: types)
}


private func cacheLayout(viewType: ViewType, type: LayoutType,
setting value: CGFloat,
constraints: () -> [NSLayoutConstraint]) {
if let caches = layoutCache[viewType]?[type] {
caches.forEach { $0.constant = value }
} else {
let constants = constraints()
constants.forEach { $0.isActive = true }
if layoutCache[viewType] == nil {
layoutCache[viewType] = [type: constants]
} else {
layoutCache[viewType]![type] = constants
}
}
}

private func layout(_ viewType: ViewType, type: LayoutType) {
let view = self.view(for: viewType)
switch type {
case .height(let v):
if let cache = layoutCache[viewType]?[type]?.first {
cache.constant = v
} else {
let constant = view.heightAnchor.constraint(equalToConstant: v)
constant.isActive = true
if layoutCache[viewType] == nil {
layoutCache[viewType] = [type: [constant]]
} else {
layoutCache[viewType]![type] = [constant]
}
cacheLayout(viewType: viewType, type: type, setting: v) { () -> [NSLayoutConstraint] in
return [view.heightAnchor.constraint(equalToConstant: v)]
}
case .width(let v):
if let cache = layoutCache[viewType]?[type]?.first {
cache.constant = v
} else {
let constant = view.widthAnchor.constraint(equalToConstant: v)
constant.isActive = true
if layoutCache[viewType] == nil {
layoutCache[viewType] = [type: [constant]]
} else {
layoutCache[viewType]![type] = [constant]
}
cacheLayout(viewType: viewType, type: type, setting: v) { () -> [NSLayoutConstraint] in
return [view.widthAnchor.constraint(equalToConstant: v)]
}
case .widthGreaterThanOrEqual(let v):
cacheLayout(viewType: viewType, type: type, setting: v) { () -> [NSLayoutConstraint] in
return [view.widthAnchor.constraint(greaterThanOrEqualToConstant: v)]
.map { item -> NSLayoutConstraint in
item.priority = .defaultHigh
return item
}
}
case .heightGreaterThanOrEqual(let v):
cacheLayout(viewType: viewType, type: type, setting: v) { () -> [NSLayoutConstraint] in
return [view.heightAnchor.constraint(greaterThanOrEqualToConstant: v)]
.map { item -> NSLayoutConstraint in
item.priority = .defaultHigh
return item
}
}
case .afterSpac(let v):
addCustomSpacing(v, after: view)
case .insets(let v):
if let caches = layoutCache[viewType]?[type] {
caches.forEach { $0.constant = v }
} else {
let constants = [view.leadingAnchor.constraint(lessThanOrEqualTo: self.leadingAnchor, constant: v),
view.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: v)]
NSLayoutConstraint.activate(constants)
layoutCache[viewType] = [type: constants]
cacheLayout(viewType: viewType, type: type, setting: v) { () -> [NSLayoutConstraint] in
return [view.leadingAnchor.constraint(lessThanOrEqualTo: self.leadingAnchor, constant: v),
view.trailingAnchor.constraint(lessThanOrEqualTo: self.trailingAnchor, constant: v)]
}

}
}

private func addCustomSpacing(_ spacing: CGFloat, after arrangedSubview: UIView) {
if #available(iOS 11.0, *) {
setCustomSpacing(spacing, after: arrangedSubview)
Expand Down
2 changes: 2 additions & 0 deletions Sources/Standard/EmptyPageForText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ open class EmptyPageForText: UILabel, EmptyPageTemplateProtocol {
func initialize() {
numberOfLines = 0
translatesAutoresizingMaskIntoConstraints = false
setContentHuggingPriority(UILayoutPriority(UILayoutPriority.defaultHigh.rawValue - 1), for: .horizontal)
setContentHuggingPriority(UILayoutPriority(UILayoutPriority.defaultHigh.rawValue - 1), for: .vertical)
font = UIFont.systemFont(ofSize: 16, weight: .medium)
textColor = UIColor(red: 102 / 255, green: 102 / 255, blue: 102 / 255, alpha: 1)
textAlignment = .center
Expand Down
6 changes: 2 additions & 4 deletions Sources/Standard/EmptyPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,42 +137,40 @@ public class EmptyPageView: UIView {
/// Vertical constraints
do {
let constraint = topGuide.heightAnchor.constraint(equalTo: bottomGuide.heightAnchor, multiplier: 1)
constraint.priority = .defaultLow
constraint.isActive = true
verticalLayout.topAndBottomHeightRatio = constraint
}

/// Horizontal constraints
do {
let constraint = leftGuide.widthAnchor.constraint(equalTo: rightGuide.widthAnchor, multiplier: 1)
constraint.priority = .defaultLow
constraint.isActive = true
horizontalLayout.leftAndRightSpacerRatio = constraint
}

/// other
do {
let constraint = contentView.leftAnchor.constraint(equalTo: leftGuide.rightAnchor, constant: 0)
constraint.priority = .defaultHigh
constraint.isActive = true
contentViewLayout.left = constraint
}

do {
let constraint = contentView.topAnchor.constraint(equalTo: topGuide.bottomAnchor, constant: 0)
constraint.priority = .defaultHigh
constraint.isActive = true
contentViewLayout.top = constraint
}

do {
let constraint = contentView.rightAnchor.constraint(equalTo: rightGuide.leftAnchor, constant: 0)
constraint.priority = .defaultHigh
constraint.isActive = true
contentViewLayout.right = constraint
}

do {
let constraint = contentView.bottomAnchor.constraint(equalTo: bottomGuide.topAnchor, constant: 0)
constraint.priority = .defaultHigh
constraint.isActive = true
contentViewLayout.bottom = constraint
}
Expand Down

0 comments on commit ca99ca0

Please sign in to comment.