-
Notifications
You must be signed in to change notification settings - Fork 1
/
UITableViewExtensions.swift
47 lines (40 loc) · 1.69 KB
/
UITableViewExtensions.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public extension UITableView {
/**
This method adjustes the height of tableHeaderView and tableFooterView,
assuming the custom header/footer views are setup using autolayout.
**Note:** Only call this method if the custom header/footer view has a
multi-line label.
**Important** this method should be called in a view controllers
`viewDidLayoutSubviews` method, like this
```
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.sizeHeaderAndFooterToFit()
}
```
*/
public func sizeHeaderAndFooterToFit() {
if let headerView = self.tableHeaderView {
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var headerFrame = headerView.frame
// If we don't have this check, viewDidLayoutSubviews() will get called
// repeatedly, thus causing the app to hang
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
self.tableHeaderView = headerView
}
}
if let footerView = self.tableFooterView {
let height = footerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var footerFrame = footerView.frame
// If we don't have this check, viewDidLayoutSubviews() will get called
// repeatedly, thus causing the app to hang
if height != footerFrame.size.height {
footerFrame.size.height = height
footerView.frame = footerFrame
self.tableFooterView = footerView
}
}
}
}