diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-05-26 10:31:07 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-05-27 16:02:24 +0200 |
| commit | fd1ec87e24e93f594fb02eaa88f7dd0ba3504eda (patch) | |
| tree | f6d029cb44c836da1519ed4a16f73462453d7182 | |
| parent | 79b5f6f600d8f1a1ffcd6ab4890064409c683061 (diff) | |
| download | mullvadvpn-fd1ec87e24e93f594fb02eaa88f7dd0ba3504eda.tar.xz mullvadvpn-fd1ec87e24e93f594fb02eaa88f7dd0ba3504eda.zip | |
SelectLocation: rework header footer view layout
| -rw-r--r-- | ios/MullvadVPN/SelectLocationHeaderView.swift | 31 | ||||
| -rw-r--r-- | ios/MullvadVPN/SelectLocationViewController.swift | 100 |
2 files changed, 62 insertions, 69 deletions
diff --git a/ios/MullvadVPN/SelectLocationHeaderView.swift b/ios/MullvadVPN/SelectLocationHeaderView.swift index f6a977ab2d..c0593418d2 100644 --- a/ios/MullvadVPN/SelectLocationHeaderView.swift +++ b/ios/MullvadVPN/SelectLocationHeaderView.swift @@ -8,7 +8,7 @@ import UIKit -class SelectLocationHeaderView: UITableViewHeaderFooterView { +class SelectLocationHeaderView: UIView { lazy var textContentLabel: UILabel = { let textLabel = UILabel() @@ -23,31 +23,24 @@ class SelectLocationHeaderView: UITableViewHeaderFooterView { var topLayoutMarginAdjustmentForNavigationBarTitle: CGFloat = 0 { didSet { let value = UIMetrics.contentLayoutMargins.top - topLayoutMarginAdjustmentForNavigationBarTitle - contentView.layoutMargins.top = max(value, 0) + layoutMargins.top = max(value, 0) } } - override init(reuseIdentifier: String?) { - super.init(reuseIdentifier: reuseIdentifier) + init() { + super.init(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) - layoutMargins = .zero - contentView.layoutMargins = UIMetrics.contentLayoutMargins - contentView.addSubview(textContentLabel) + backgroundColor = .secondaryColor + layoutMargins = UIMetrics.contentLayoutMargins + insetsLayoutMarginsFromSafeArea = false - backgroundView = UIView() - backgroundView?.backgroundColor = .secondaryColor - - let trailingConstraint = textContentLabel.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor) - trailingConstraint.priority = UILayoutPriority(800) - - let bottomConstraint = textContentLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor) - bottomConstraint.priority = UILayoutPriority(800) + addSubview(textContentLabel) NSLayoutConstraint.activate([ - textContentLabel.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor), - textContentLabel.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor), - trailingConstraint, - bottomConstraint + textContentLabel.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor), + textContentLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor), + textContentLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor), + textContentLabel.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor) ]) } diff --git a/ios/MullvadVPN/SelectLocationViewController.swift b/ios/MullvadVPN/SelectLocationViewController.swift index 18f3b792ae..947b138863 100644 --- a/ios/MullvadVPN/SelectLocationViewController.swift +++ b/ios/MullvadVPN/SelectLocationViewController.swift @@ -15,13 +15,14 @@ protocol SelectLocationViewControllerDelegate: AnyObject { class SelectLocationViewController: UIViewController, UITableViewDelegate { - private enum ReuseIdentifiers: String { - case cell - case header - } + static let cellReuseIdentifier = "Cell" private var tableView: UITableView? + private let tableHeaderFooterView = SelectLocationHeaderView() + private var tableHeaderFooterViewTopConstraints: [NSLayoutConstraint] = [] + private var tableHeaderFooterViewBottomConstraints: [NSLayoutConstraint] = [] + private let logger = Logger(label: "SelectLocationController") private var dataSource: LocationDataSource? private var setCachedRelaysOnViewDidLoad: CachedRelays? @@ -31,7 +32,7 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { private var showHeaderViewAtTheBottom = false { didSet { - setTableHeaderFooterDimensions() + setTableHeaderFooterConstraints() } } @@ -52,31 +53,30 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { super.viewDidLoad() let tableView = UITableView(frame: view.bounds, style: .plain) - tableView.translatesAutoresizingMaskIntoConstraints = true - tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight] + tableView.translatesAutoresizingMaskIntoConstraints = false tableView.backgroundColor = .clear tableView.separatorColor = .secondaryColor tableView.separatorInset = .zero tableView.estimatedRowHeight = 53 tableView.indicatorStyle = .white - tableView.register(SelectLocationHeaderView.self, forHeaderFooterViewReuseIdentifier: ReuseIdentifiers.header.rawValue) - tableView.register(SelectLocationCell.self, forCellReuseIdentifier: ReuseIdentifiers.cell.rawValue) + tableView.register(SelectLocationCell.self, forCellReuseIdentifier: Self.cellReuseIdentifier) self.tableView = tableView - setTableHeaderFooterDimensions() - view.backgroundColor = .secondaryColor view.addSubview(tableView) + tableHeaderFooterView.translatesAutoresizingMaskIntoConstraints = false + view.addSubview(tableHeaderFooterView) + dataSource = LocationDataSource( tableView: tableView, cellProvider: { [weak self] (tableView, indexPath, item) -> UITableViewCell? in guard let self = self else { return nil } - let cell = tableView.dequeueReusableCell( - withIdentifier: ReuseIdentifiers.cell.rawValue, for: indexPath) as! SelectLocationCell + let cell = tableView.dequeueReusableCell(withIdentifier: Self.cellReuseIdentifier, for: indexPath) + as! SelectLocationCell cell.accessibilityIdentifier = item.location.stringRepresentation cell.isDisabled = !item.isActive @@ -94,6 +94,25 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { tableView.delegate = self tableView.dataSource = dataSource + tableHeaderFooterViewTopConstraints = [ + tableHeaderFooterView.topAnchor.constraint(equalTo: view.topAnchor), + tableView.topAnchor.constraint(equalTo: tableHeaderFooterView.bottomAnchor), + tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor) + ] + tableHeaderFooterViewBottomConstraints = [ + tableHeaderFooterView.bottomAnchor.constraint(equalTo: view.bottomAnchor), + tableView.topAnchor.constraint(equalTo: view.topAnchor), + tableView.bottomAnchor.constraint(equalTo: tableHeaderFooterView.topAnchor) + ] + + NSLayoutConstraint.activate([ + tableHeaderFooterView.leadingAnchor.constraint(equalTo: view.leadingAnchor), + tableHeaderFooterView.trailingAnchor.constraint(equalTo: view.trailingAnchor), + tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor), + tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor) + ]) + setTableHeaderFooterConstraints() + if let setCachedRelaysOnViewDidLoad = self.setCachedRelaysOnViewDidLoad { dataSource?.setRelays(setCachedRelaysOnViewDidLoad.relays) } @@ -144,6 +163,12 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { } } + override func viewDidLayoutSubviews() { + super.viewDidLayoutSubviews() + + updateTableHeaderTopLayoutMargin() + } + // MARK: - UITableViewDelegate func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool { @@ -173,32 +198,6 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { self.delegate?.selectLocationViewController(self, didSelectRelayLocation: item.location) } - func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - assert(section == 0) - - if showHeaderViewAtTheBottom { - return nil - } else { - let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: ReuseIdentifiers.header.rawValue) as! SelectLocationHeaderView - updateTopLayoutMargin(forHeaderView: view) - - return view - } - } - - func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? { - assert(section == 0) - - if showHeaderViewAtTheBottom { - let view = tableView.dequeueReusableHeaderFooterView(withIdentifier: ReuseIdentifiers.header.rawValue) as! SelectLocationHeaderView - view.topLayoutMarginAdjustmentForNavigationBarTitle = 0 - - return view - } else { - return nil - } - } - // MARK: - Public func setCachedRelays(_ cachedRelays: CachedRelays) { @@ -237,25 +236,26 @@ class SelectLocationViewController: UIViewController, UITableViewDelegate { // MARK: - Private - private func updateTopLayoutMargin(forHeaderView view: SelectLocationHeaderView) { + private func updateTableHeaderTopLayoutMargin() { // When contained within the navigation controller, we want the distance between the navigation title // and the table header label to be exactly 24pt. - if let navigationBar = navigationController?.navigationBar as? CustomNavigationBar { - view.topLayoutMarginAdjustmentForNavigationBarTitle = navigationBar.titleLabelBottomInset + if let navigationBar = navigationController?.navigationBar as? CustomNavigationBar, !showHeaderViewAtTheBottom { + tableHeaderFooterView.topLayoutMarginAdjustmentForNavigationBarTitle = navigationBar.titleLabelBottomInset } else { - view.topLayoutMarginAdjustmentForNavigationBarTitle = 0 + tableHeaderFooterView.topLayoutMarginAdjustmentForNavigationBarTitle = 0 } } - private func setTableHeaderFooterDimensions() { - let headerFooterHeight: CGFloat = 109 - + private func setTableHeaderFooterConstraints() { if showHeaderViewAtTheBottom { - self.tableView?.estimatedSectionHeaderHeight = 0 - self.tableView?.estimatedSectionFooterHeight = headerFooterHeight + NSLayoutConstraint.deactivate( + tableHeaderFooterViewTopConstraints) + NSLayoutConstraint.activate(tableHeaderFooterViewBottomConstraints) } else { - self.tableView?.estimatedSectionHeaderHeight = headerFooterHeight - self.tableView?.estimatedSectionFooterHeight = 0 + NSLayoutConstraint.deactivate( + tableHeaderFooterViewBottomConstraints) + NSLayoutConstraint.activate(tableHeaderFooterViewTopConstraints) } + view.layoutIfNeeded() } } |
