Skip to content

Commit

Permalink
Bugfix: SectionFooterView AutoDimension Support (#110)
Browse files Browse the repository at this point in the history
* Revert "fixes footer and header heights on ios 11 to be automatic dimension if the view returns as 0 (#109)"

This reverts commit 319a8b8.

* Revert "Merge pull request #108 from nealyoung/extremity-height-fix"

This reverts commit 2071110, reversing
changes made to e002d86.

* Bugfix: SectionFooterView AutoDimension Support

Problem:
1. I noticed our app was manually laying out subviews in our sectionFooters when I didn't see the need to.

2. After recent changes, Group tableView's which had nil section extremity defaulted to zero, which drops the standard spacing between sections. Note in attatchment.

Notable info:
Statics currently has deploy target of iOS8, which did not yet support automatic sectionFooterView dimensioning.

Solution:
- Offer autosizing option which uses UITableViewAutomaticDimension rather than view's original bounds height. Retain original .view(_) case in the event some clients were already sizing beforehand limiting integration breaks. This also offers automatic margin adherence.
- Bump deploy target to iOS9 for clean integration.
- Revert 2 previous PRs
- Default height for plain/grouped styles to play friendly with tableView's defaults.

* Example: AutoSized Extremity
  • Loading branch information
dmiluski authored and rhaining committed Oct 4, 2017
1 parent d2e0750 commit 8ae55ef
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 19 deletions.
33 changes: 32 additions & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ViewController: TableViewController {

tableView.rowHeight = 50

// Note:
// Required to be set pre iOS11, to support autosizing
tableView.estimatedSectionHeaderHeight = 13.5
tableView.estimatedSectionFooterHeight = 13.5

dataSource.sections = [
Section(header: "Styles", rows: [
Row(text: "Value 1", detailText: "Detail", cellClass: Value1Cell.self),
Expand Down Expand Up @@ -69,7 +74,8 @@ class ViewController: TableViewController {
self.showAlert(title: "Deleted.")
})
])
])
]),
Section(header: "AutoSized SectionFooterView", rows: [], footer: Section.Extremity.autoLayoutView(LargeAutoSizedExtremityView()))
]
}

Expand All @@ -82,3 +88,28 @@ class ViewController: TableViewController {
present(alert, animated: true, completion: nil)
}
}

class LargeAutoSizedExtremityView: UIView {
lazy var label: UILabel = {
let label = UILabel()
label.numberOfLines = 0
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Is this the real life?\nIs this just fantasy?\nCaught in a landslide,\nNo escape from reality."
return label
}()

init() {
super.init(frame: .zero)

layoutMargins = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
addSubview(label)
label.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor).isActive = true
label.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor).isActive = true
label.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
4 changes: 2 additions & 2 deletions Static.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -493,7 +493,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 3.0.1;
Expand Down
25 changes: 11 additions & 14 deletions Static/DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,7 @@ extension DataSource: UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, heightForHeaderInSection sectionIndex: Int) -> CGFloat {
guard let headerView = section(at: sectionIndex)?.header else { return 0 }

var headerHeight = headerView.viewHeight
if headerHeight == 0 {
headerHeight = UITableViewAutomaticDimension
}
return headerHeight
return section(at: sectionIndex)?.header?.viewHeight ?? tableView.style.defaultSectionExtremityHeight
}

public func tableView(_ tableView: UITableView, titleForFooterInSection sectionIndex: Int) -> String? {
Expand All @@ -222,13 +216,7 @@ extension DataSource: UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, heightForFooterInSection sectionIndex: Int) -> CGFloat {
guard let footerView = section(at: sectionIndex)?.footer else { return 0 }

var footerHeight = footerView.viewHeight
if footerHeight == 0 {
footerHeight = UITableViewAutomaticDimension
}
return footerHeight
return section(at: sectionIndex)?.footer?.viewHeight ?? tableView.style.defaultSectionExtremityHeight
}

public func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
Expand Down Expand Up @@ -295,3 +283,12 @@ extension DataSource: UITableViewDelegate {
}
}
}

extension UITableViewStyle {
var defaultSectionExtremityHeight: CGFloat {
switch self {
case .plain: return 0
case .grouped: return UITableViewAutomaticDimension
}
}
}
9 changes: 7 additions & 2 deletions Static/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ public struct Section: Hashable, Equatable {
/// Custom view for the header or footer. The height will be the view's `bounds.height`.
case view(UIView)

// View Sized with autolayout
// If Pre iOS11: Requires tableview estimatedSectionHeader/FooterHeight to be > 0
case autoLayoutView(UIView)

var _title: String? {
switch self {
case .title(let extremityTitle): return extremityTitle
Expand All @@ -23,14 +27,15 @@ public struct Section: Hashable, Equatable {
var _view: UIView? {
switch self {
case .view(let extremityView): return extremityView
case .autoLayoutView(let extremityView): return extremityView
default: return nil
}
}

var viewHeight: CGFloat {
switch self {
case .title: return UITableViewAutomaticDimension
case .view(let extremityView): return extremityView.bounds.height
case .title(_), .autoLayoutView(_): return UITableViewAutomaticDimension
case .view(let view): return view.bounds.height
}
}
}
Expand Down

0 comments on commit 8ae55ef

Please sign in to comment.