material-components_materia.../components/AppBar/examples/AppBarDelegateForwardingExample.swift
Adrian Secord 8c3af252be Adding AUTHORS and removing CONTRIBUTORS.txt.
We can credit any contributor who would like to be credited this way, by adding them on request to the AUTHORS file. The copyright statement changes are required for this to work. Note that this has...

Summary:

We can credit any contributor who would like to be credited this way, by adding them on request to the AUTHORS file. The copyright statement changes are required for this to work. Note that this has no legal change, since the contributors always retained their copyright despite the copyright notice, but it's a nice acknowledgement.

Changed copyright statement to include non-Google authors.

Command run:

find * \( -name '*\.m' -or -name '*\.h' -or -name '*\.swift' \) -and -not \( -path 'scripts/external*' -name Pods  \) -print0 | xargs -0 sed -i '' 's/Copyright \(.*\) Google Inc/Copyright \1 the Material Components for iOS authors/'

Added non-source files.

Command run:

grep -Rl 'Copyright .* Google Inc' * --exclude-dir scripts/external --null | xargs -0 sed -i '' 's/Copyright \(.*\) Google Inc/Copyright \1 the Material Components for iOS authors/'

Reviewers: featherless, O1 Material components iOS, randallli

Reviewed By: O1 Material components iOS, randallli

Tags: #material_components_ios

Differential Revision: http://codereview.cc/D1415
2016-08-08 08:16:37 -07:00

161 lines
5.0 KiB
Swift

/*
Copyright 2016-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 Foundation
import MaterialComponents
// This example builds upon AppBarTypicalUseExample.
class AppBarDelegateForwardingExample: UITableViewController {
let appBar = MDCAppBar()
convenience init() {
self.init(style: .Plain)
}
override init(style: UITableViewStyle) {
super.init(style: style)
self.appBar.navigationBar.tintColor = UIColor.whiteColor()
self.addChildViewController(appBar.headerViewController)
self.title = "Delegate Forwarding"
let color = UIColor(
red: CGFloat(0x03) / CGFloat(255),
green: CGFloat(0xA9) / CGFloat(255),
blue: CGFloat(0xF4) / CGFloat(255),
alpha: 1)
appBar.headerViewController.headerView.backgroundColor = color
appBar.navigationBar.tintColor = UIColor.whiteColor()
}
override func viewDidLoad() {
super.viewDidLoad()
// UITableViewController's tableView.delegate is self by default. We're setting it here for
// emphasis.
self.tableView.delegate = self
appBar.headerViewController.headerView.trackingScrollView = self.tableView
appBar.addSubviewsToParent()
self.tableView.layoutMargins = UIEdgeInsetsZero
self.tableView.separatorInset = UIEdgeInsetsZero
}
// The following four methods must be forwarded to the tracking scroll view in order to implement
// the Flexible Header's behavior.
override func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == self.appBar.headerViewController.headerView.trackingScrollView {
self.appBar.headerViewController.headerView.trackingScrollViewDidScroll()
}
}
override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
if scrollView == self.appBar.headerViewController.headerView.trackingScrollView {
self.appBar.headerViewController.headerView.trackingScrollViewDidEndDecelerating()
}
}
override func scrollViewDidEndDragging(scrollView: UIScrollView, willDecelerate decelerate: Bool) {
let headerView = self.appBar.headerViewController.headerView
if scrollView == headerView.trackingScrollView {
headerView.trackingScrollViewDidEndDraggingWillDecelerate(decelerate)
}
}
override func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let headerView = self.appBar.headerViewController.headerView
if scrollView == headerView.trackingScrollView {
headerView.trackingScrollViewWillEndDraggingWithVelocity(velocity, targetContentOffset: targetContentOffset)
}
}
// MARK: Typical setup
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
self.title = "Delegate Forwarding (Swift)"
self.addChildViewController(appBar.headerViewController)
let color = UIColor(
red: CGFloat(0x03) / CGFloat(255),
green: CGFloat(0xA9) / CGFloat(255),
blue: CGFloat(0xF4) / CGFloat(255),
alpha: 1)
appBar.headerViewController.headerView.backgroundColor = color
appBar.navigationBar.tintColor = UIColor.whiteColor()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
// MARK: Catalog by convention
extension AppBarDelegateForwardingExample {
class func catalogBreadcrumbs() -> [String] {
return ["App Bar", "Delegate Forwarding (Swift)"]
}
func catalogShouldHideNavigation() -> Bool {
return true
}
}
extension AppBarDelegateForwardingExample {
override func childViewControllerForStatusBarHidden() -> UIViewController? {
return appBar.headerViewController
}
override func childViewControllerForStatusBarStyle() -> UIViewController? {
return appBar.headerViewController
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: animated)
}
}
// MARK: - Typical application code (not Material-specific)
// MARK: UITableViewDataSource
extension AppBarDelegateForwardingExample {
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
override func tableView(
tableView: UITableView,
cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = self.tableView.dequeueReusableCellWithIdentifier("cell")
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: "cell")
}
cell!.layoutMargins = UIEdgeInsetsZero
return cell!
}
}