Skip to content

Commit

Permalink
Cleanup and transparent tableView example
Browse files Browse the repository at this point in the history
  • Loading branch information
PimCoumans committed Apr 19, 2023
1 parent eaa4584 commit 2cad040
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class ViewController: UIViewController {
case newSimple
case resizingView
case bigResizingView
case newSimpleNav
}

let stackView = UIStackView()
Expand Down Expand Up @@ -114,12 +113,6 @@ class ViewController: UIViewController {
panelPresenter.viewController = viewController
panelPresenter.present(from: self)
return
case .newSimpleNav:
viewController = UINavigationController(rootViewController: NewSimpleViewController())
let panelPresenter = PanelPresenter(viewController: viewController)
panelPresenter.viewController = viewController
panelPresenter.present(from: self)
return
}
present(viewController, animated: true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ResizingViewController: UIViewController, PanelPresentable {
let multilineLabel = UILabel()
multilineLabel.text = "To make use of the behavior that PanelPresenter provides, make sure your view controller conforms to PanelPresentable and set the presenter’s viewController property to self in your initializer. Doing this at a later stage will result in weird stuff.\nJust add your views to your view, which will be added to panelPresenter's scroll view. And any navigation-type views can be placed in the headerContentView which will be displayed above your content and will stick to the top of the screen when scrolling."
multilineLabel.numberOfLines = 0
// multilineLabel.backgroundColor = .white
multilineLabel.textColor = .label
multilineLabel.contentMode = .top
view.addSubview(multilineLabel)
Expand All @@ -31,34 +30,34 @@ class ResizingViewController: UIViewController, PanelPresentable {
}()

let numberOfCells: Int

init(cellCount: Int = 8) {
numberOfCells = cellCount
super.init(nibName: nil, bundle: nil)
}
private lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.bounds, style: .plain)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
super.init(nibName: nil, bundle: nil)
}

private lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.bounds, style: .plain)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
private lazy var titleView: UILabel = {
let label = UILabel()
label.text = "Some TableView"
label.textColor = .label
label.font = .preferredFont(forTextStyle: .title2)
label.adjustsFontForContentSizeCategory = true
return label
}()
tableView.dataSource = self
return tableView
}()

private lazy var titleView: UILabel = {
let label = UILabel()
label.text = "Some TableView"
label.textColor = .label
label.font = .preferredFont(forTextStyle: .title2)
label.adjustsFontForContentSizeCategory = true
return label
}()

private lazy var doneButton: UIButton = compatibleButton(title: "Done", selector: #selector(didPressDoneButton))
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

@objc func toggleView() {
showingTableView.toggle()
Expand Down Expand Up @@ -93,9 +92,9 @@ class ResizingViewController: UIViewController, PanelPresentable {
self.panelPresentationController?.setNeedsScrollViewUpdate()
}
}
override func viewDidLoad() {
super.viewDidLoad()

override func viewDidLoad() {
super.viewDidLoad()

panelPresentationController?.showsHeader = true
let titleLabel = UILabel()
Expand All @@ -105,17 +104,17 @@ class ResizingViewController: UIViewController, PanelPresentable {
panelPresentationController?.headerView.addSubview(titleLabel)
titleLabel.extendToSuperviewLayoutMargins()

view.addSubview(tableView)
view.addSubview(tableView)
tableView.backgroundColor = .clear
tableView.extendToSuperview()
tableView.extendToSuperview()
panelPresentationController?.topInset = 20
}
}
}

extension ResizingViewController {
@objc func didPressDoneButton(button: UIButton) {
presentingViewController?.dismiss(animated: true)
}
@objc func didPressDoneButton(button: UIButton) {
presentingViewController?.dismiss(animated: true)
}
}

extension ResizingViewController: UITableViewDelegate {
Expand All @@ -126,17 +125,17 @@ extension ResizingViewController: UITableViewDelegate {
}

extension ResizingViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
numberOfCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "Table Cell \(indexPath.row)"
cell.backgroundColor = .clear
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
numberOfCells
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

cell.textLabel?.text = "Table Cell \(indexPath.row)"
cell.backgroundColor = .clear
cell.textLabel?.textColor = .label
return cell
}

return cell
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,93 @@ import PanelPresenter
import ConstraintBuilder

class TableViewController: UIViewController, PanelPresentable {
let panelPresenter = PanelPresenter()
var panelScrollView: UIScrollView? {
tableView
}

let panelPresenter = PanelPresenter()

var panelScrollView: UIScrollView? {
tableView
}

let numberOfCells: Int

init(cellCount: Int = 8) {
numberOfCells = cellCount
super.init(nibName: nil, bundle: nil)
panelPresenter.viewController = self
}

private lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.bounds, style: .plain)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.dataSource = self
return tableView
}()

private lazy var titleView: UILabel = {
let label = UILabel()
label.text = "Some TableView"
label.textColor = .label
label.font = .preferredFont(forTextStyle: .title2)
label.adjustsFontForContentSizeCategory = true
return label
}()

super.init(nibName: nil, bundle: nil)
panelPresenter.viewController = self
}

private lazy var tableView: UITableView = {
let tableView = UITableView(frame: self.view.bounds, style: .plain)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.dataSource = self
tableView.backgroundColor = .clear
return tableView
}()

private lazy var titleView: UILabel = {
let label = UILabel()
label.text = "Some TableView"
label.textColor = .label
label.font = .preferredFont(forTextStyle: .title2)
label.adjustsFontForContentSizeCategory = true
return label
}()

private lazy var doneButton: UIButton = compatibleButton(title: "Done", selector: #selector(didPressDoneButton))
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.extendToSuperview()

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.extendToSuperview()

guard let headerView = panelPresentationController?.headerView else {
return
}
headerView.addSubview(titleView)
headerView.addSubview(doneButton)
doneButton.applyConstraints {
$0.trailingAnchor.constraint(equalTo: headerView.layoutMarginsGuide.trailingAnchor)
$0.topAnchor.constraint(equalTo: headerView.layoutMarginsGuide.topAnchor)
$0.bottomAnchor.constraint(equalTo: headerView.layoutMarginsGuide.bottomAnchor)
}
titleView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
let centerX = titleView.centerXAnchor.constraint(equalTo: headerView.layoutMarginsGuide.centerXAnchor)
centerX.priority = .defaultLow
titleView.applyConstraints {
$0.topAnchor.constraint(equalTo: headerView.layoutMarginsGuide.topAnchor)
$0.bottomAnchor.constraint(equalTo: headerView.layoutMarginsGuide.bottomAnchor)
$0.trailingAnchor.constraint(lessThanOrEqualTo: doneButton.leadingAnchor, constant: 10)
$0.leadingAnchor.constraint(greaterThanOrEqualTo: headerView.layoutMarginsGuide.leadingAnchor)
centerX
}
}
headerView.addSubview(titleView)
headerView.addSubview(doneButton)
doneButton.applyConstraints {
$0.trailingAnchor.constraint(equalTo: headerView.layoutMarginsGuide.trailingAnchor)
$0.topAnchor.constraint(equalTo: headerView.layoutMarginsGuide.topAnchor)
$0.bottomAnchor.constraint(equalTo: headerView.layoutMarginsGuide.bottomAnchor)
}

titleView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
let centerX = titleView.centerXAnchor.constraint(equalTo: headerView.layoutMarginsGuide.centerXAnchor)
centerX.priority = .defaultLow
titleView.applyConstraints {
$0.topAnchor.constraint(equalTo: headerView.layoutMarginsGuide.topAnchor)
$0.bottomAnchor.constraint(equalTo: headerView.layoutMarginsGuide.bottomAnchor)
$0.trailingAnchor.constraint(lessThanOrEqualTo: doneButton.leadingAnchor, constant: 10)
$0.leadingAnchor.constraint(greaterThanOrEqualTo: headerView.layoutMarginsGuide.leadingAnchor)
centerX
}
}
}

extension TableViewController {
@objc func didPressDoneButton(button: UIButton) {
presentingViewController?.dismiss(animated: true)
}
@objc func didPressDoneButton(button: UIButton) {
presentingViewController?.dismiss(animated: true)
}
}

extension TableViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
numberOfCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
numberOfCells
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = .clear
let textField = cell.contentView.subviews.lazy.compactMap { $0 as? UITextField }.first ?? UITextField()
textField.setContentHuggingPriority(.defaultLow, for: .vertical)
textField.text = "Table Cell \(indexPath.row)"
cell.contentView.addSubview(textField)
textField.extendToSuperviewLayoutMargins()
return cell
}

return cell
}
}

0 comments on commit 2cad040

Please sign in to comment.