mirror of
https://github.com/material-components/material-components-ios.git
synced 2026-02-20 08:27:32 +08:00
The prior names of composed vs inherited didn't meaningfully translate to the intended purpose of the two view controllers, which is to provide harnesses within which the flexible header can be placed either as a child or as a sibling to its tracking scroll view. This distinction is important because the flexible header behaves differently when it is a child of its tracking scroll view, as is the case when using UITableViewController. PiperOrigin-RevId: 310869953
101 lines
3.2 KiB
Swift
101 lines
3.2 KiB
Swift
// Copyright 2019-present the Material Components for iOS authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
import UIKit
|
|
import MaterialComponents.MaterialFlexibleHeader
|
|
|
|
/// A table view controller that adds a UITableView as a child of its view.
|
|
///
|
|
/// The purpose of this view controller is to demonstrate how the flexible header view interacts
|
|
/// with view controllers whose self.view is *not* the tracking scroll view. In cases like these,
|
|
/// the flexible header view can be added as a sibling to the tracking scroll view.
|
|
class SiblingOfTrackingScrollViewViewController: UIViewController {
|
|
|
|
init(title: String? = nil) {
|
|
super.init(nibName: nil, bundle: nil)
|
|
self.title = title
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("NSCoding unsupported")
|
|
}
|
|
|
|
var tableView = UITableView(frame: CGRect(), style: .plain)
|
|
|
|
var headerView: MDCFlexibleHeaderView?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
self.view.addSubview(tableView)
|
|
self.tableView.frame = self.view.bounds
|
|
|
|
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
|
|
tableView.delegate = self
|
|
tableView.dataSource = self
|
|
|
|
view.isOpaque = false
|
|
view.backgroundColor = .white
|
|
}
|
|
}
|
|
|
|
extension SiblingOfTrackingScrollViewViewController: UITableViewDataSource {
|
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
return 1
|
|
}
|
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
return 100
|
|
}
|
|
}
|
|
|
|
extension SiblingOfTrackingScrollViewViewController: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
|
|
let titleString = title ?? ""
|
|
cell.textLabel?.text = "\(titleString): Row \(indexPath.item)"
|
|
return cell
|
|
}
|
|
|
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
|
headerView?.trackingScrollDidScroll()
|
|
}
|
|
|
|
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
|
|
headerView?.trackingScrollDidEndDecelerating()
|
|
}
|
|
|
|
func scrollViewWillEndDragging(
|
|
_ scrollView: UIScrollView,
|
|
withVelocity velocity: CGPoint,
|
|
targetContentOffset: UnsafeMutablePointer<CGPoint>
|
|
) {
|
|
headerView?.trackingScrollWillEndDragging(
|
|
withVelocity: velocity,
|
|
targetContentOffset: targetContentOffset)
|
|
}
|
|
|
|
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
|
|
headerView?.trackingScrollDidEndDraggingWillDecelerate(decelerate)
|
|
}
|
|
|
|
func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView) {
|
|
if #available(iOS 11.0, *) {
|
|
headerView?.trackingScrollDidChangeAdjustedContentInset(scrollView)
|
|
}
|
|
}
|
|
}
|