Skip to content

Commit 7b88c3a

Browse files
committed
optimize custom empty data view layout
1 parent d38d7fd commit 7b88c3a

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

Example/EmptyDataDemoCollectionViewController.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class EmptyDataDemoCollectionViewController: UICollectionViewController, TBEmpty
118118

119119
func customViewForEmptyDataSet(scrollView: UIScrollView!) -> UIView? {
120120
let loadingView: UIView = {
121+
if indexPath.row == 2 {
122+
return ExampleCustomView(frame: CGRect.zero)
123+
}
124+
121125
let loadingImageView = UIImageView(image: UIImage(named: "loading")!)
122126
let view = UIView(frame: loadingImageView.frame)
123127
view.addSubview(loadingImageView)
@@ -208,3 +212,41 @@ extension EmptyDataDemoCollectionViewController: UICollectionViewDelegateFlowLay
208212
}
209213
}
210214
}
215+
216+
class ExampleCustomView: UIView {
217+
private lazy var contentLabel: UILabel = {
218+
let contentLabel = UILabel()
219+
contentLabel.numberOfLines = 0
220+
contentLabel.textColor = UIColor.whiteColor()
221+
contentLabel.translatesAutoresizingMaskIntoConstraints = false
222+
return contentLabel
223+
}()
224+
225+
override init(frame: CGRect) {
226+
super.init(frame: frame)
227+
commonInit()
228+
}
229+
230+
required init?(coder aDecoder: NSCoder) {
231+
super.init(coder: aDecoder)
232+
commonInit()
233+
}
234+
235+
private func commonInit() {
236+
backgroundColor = UIColor.lightGrayColor()
237+
layer.cornerRadius = 6
238+
239+
let activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .WhiteLarge)
240+
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
241+
activityIndicatorView.startAnimating()
242+
contentLabel.text = "Loading... Please wait a moment...\n\n(This is a custom empty data view, which is using pure AutoLayout)"
243+
addSubview(activityIndicatorView)
244+
addSubview(contentLabel)
245+
246+
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-15-[activityIndicatorView]-20-[contentLabel]-15-|", options: [], metrics: nil, views: ["activityIndicatorView": activityIndicatorView, "contentLabel": contentLabel]))
247+
addConstraint(NSLayoutConstraint(item: activityIndicatorView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
248+
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[contentLabel]-15-|", options: [], metrics: nil, views: ["contentLabel": contentLabel]))
249+
250+
translatesAutoresizingMaskIntoConstraints = false
251+
}
252+
}

Example/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.7</string>
18+
<string>1.8</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>
22-
<string>8</string>
22+
<string>9</string>
2323
<key>LSRequiresIPhoneOS</key>
2424
<true/>
2525
<key>UILaunchStoryboardName</key>

TBEmptyDataSet.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "TBEmptyDataSet"
4-
s.version = "1.7"
4+
s.version = "1.8"
55
s.summary = "An extension of UITableView/UICollectionView's super class, it will display a placeholder when the data is empty."
66

77
s.homepage = "https://github.com/teambition/TBEmptyDataSet"

TBEmptyDataSet/EmptyDataView.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,13 @@ class EmptyDataView: UIView {
6464
var tapGesture: UITapGestureRecognizer!
6565
var customView: UIView? {
6666
willSet {
67-
if let _ = customView {
68-
customView!.removeFromSuperview()
67+
if let customView = customView {
68+
customView.removeFromSuperview()
6969
}
7070
}
7171
didSet {
72-
if let _ = customView {
73-
customView!.translatesAutoresizingMaskIntoConstraints = false
74-
contentView.addSubview(customView!)
72+
if let customView = customView {
73+
contentView.addSubview(customView)
7574
}
7675
}
7776
}
@@ -133,11 +132,22 @@ class EmptyDataView: UIView {
133132
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[\(ViewStrings.contentView)]|", options: [], metrics: nil, views: [ViewStrings.contentView : contentView]))
134133

135134
if let customView = customView {
136-
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0))
137-
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .CenterY, relatedBy: .Equal, toItem: contentView, attribute: .CenterY, multiplier: 1, constant: 0))
138-
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Width, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: customView.frame.width))
139-
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Height, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: customView.frame.height))
140-
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(ViewStrings.customView)]|", options: [], metrics: nil, views: [ViewStrings.customView: customView]))
135+
let translatesFrameIntoConstraints = customView.translatesAutoresizingMaskIntoConstraints
136+
customView.translatesAutoresizingMaskIntoConstraints = false
137+
138+
if translatesFrameIntoConstraints {
139+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Width, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: customView.frame.width))
140+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Height, relatedBy: .Equal, toItem: .None, attribute: .NotAnAttribute, multiplier: 1, constant: customView.frame.height))
141+
142+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0))
143+
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(ViewStrings.customView)]|", options: [], metrics: nil, views: [ViewStrings.customView: customView]))
144+
} else {
145+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .CenterX, relatedBy: .Equal, toItem: contentView, attribute: .CenterX, multiplier: 1, constant: 0))
146+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Leading, relatedBy: .GreaterThanOrEqual, toItem: contentView, attribute: .Leading, multiplier: 1, constant: 0))
147+
contentView.addConstraint(NSLayoutConstraint(item: customView, attribute: .Trailing, relatedBy: .LessThanOrEqual, toItem: contentView, attribute: .Trailing, multiplier: 1, constant: 0))
148+
149+
contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[\(ViewStrings.customView)]|", options: [], metrics: nil, views: [ViewStrings.customView: customView]))
150+
}
141151
} else {
142152
var viewStrings = [String]()
143153
var views = [String: UIView]()

TBEmptyDataSet/Supporting Files/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.7</string>
18+
<string>1.8</string>
1919
<key>CFBundleSignature</key>
2020
<string>????</string>
2121
<key>CFBundleVersion</key>

0 commit comments

Comments
 (0)