diff --git a/Demo/ScrollingNavbarDemo.xcodeproj/project.pbxproj b/Demo/ScrollingNavbarDemo.xcodeproj/project.pbxproj index cd14e7f4..2e791263 100644 --- a/Demo/ScrollingNavbarDemo.xcodeproj/project.pbxproj +++ b/Demo/ScrollingNavbarDemo.xcodeproj/project.pbxproj @@ -226,7 +226,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0700; - LastUpgradeCheck = 0810; + LastUpgradeCheck = 0820; ORGANIZATIONNAME = "Fancy Pixel"; TargetAttributes = { 652BE2CD1B62805C00DE75C7 = { diff --git a/Demo/ScrollingNavbarDemo.xcodeproj/xcshareddata/xcschemes/ScrollingNavbarDemo.xcscheme b/Demo/ScrollingNavbarDemo.xcodeproj/xcshareddata/xcschemes/ScrollingNavbarDemo.xcscheme index 7fb42828..55b42d24 100644 --- a/Demo/ScrollingNavbarDemo.xcodeproj/xcshareddata/xcschemes/ScrollingNavbarDemo.xcscheme +++ b/Demo/ScrollingNavbarDemo.xcodeproj/xcshareddata/xcschemes/ScrollingNavbarDemo.xcscheme @@ -1,6 +1,6 @@ - + + + + + - + + @@ -15,14 +19,14 @@ - + - + - + @@ -64,24 +68,24 @@ - + - + - + - + @@ -97,17 +101,17 @@ - + - + @@ -123,17 +127,17 @@ - + - + @@ -149,17 +153,17 @@ - + - + @@ -197,12 +201,12 @@ - + - - + + @@ -211,7 +215,7 @@ - + @@ -224,7 +228,6 @@ - @@ -241,7 +244,7 @@ - + @@ -266,31 +269,31 @@ - + - - - + + + - + - + - + @@ -298,18 +301,42 @@ + + + + + + + + + + + + + + + + + + + + - + + + + + @@ -325,18 +352,18 @@ - + - - + + - + diff --git a/Demo/ScrollingNavbarDemo/ViewControllers/TableViewController.swift b/Demo/ScrollingNavbarDemo/ViewControllers/TableViewController.swift index 6456122b..f83f7f35 100644 --- a/Demo/ScrollingNavbarDemo/ViewControllers/TableViewController.swift +++ b/Demo/ScrollingNavbarDemo/ViewControllers/TableViewController.swift @@ -12,11 +12,15 @@ import AMScrollingNavbar class TableViewController: ScrollingNavigationViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var tableView: UITableView! + @IBOutlet weak var toolbar: UIToolbar! override func viewDidLoad() { super.viewDidLoad() title = "TableView" + tableView.contentInset = UIEdgeInsets(top: 44, left: 0, bottom: 0, right: 0) + toolbar.barTintColor = UIColor(red:0.91, green:0.3, blue:0.24, alpha:1) + toolbar.tintColor = .white navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: nil, action: nil) navigationController?.navigationBar.barTintColor = UIColor(red:0.91, green:0.3, blue:0.24, alpha:1) } @@ -26,7 +30,8 @@ class TableViewController: ScrollingNavigationViewController, UITableViewDelegat super.viewDidAppear(animated) if let navigationController = self.navigationController as? ScrollingNavigationController { - navigationController.followScrollView(tableView, delay: 50.0) + navigationController.followScrollView(tableView, delay: 50.0, followers: [toolbar]) + navigationController.scrollingNavbarDelegate = self } } @@ -43,3 +48,9 @@ class TableViewController: ScrollingNavigationViewController, UITableViewDelegat } } + +extension TableViewController: ScrollingNavigationControllerDelegate { + func scrollingNavigationController(_ controller: ScrollingNavigationController, willChangeState state: NavigationBarState) { + view.needsUpdateConstraints() + } +} diff --git a/README.md b/README.md index 712141d6..6e01c075 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ scrolling of an observed content view Version `2.x` is written as a subclass of `UINavigationController`, in Swift. Version `2.0.0` introduce Swift 2.0 syntax. -Version `3.0.0` introduce Swift 3.0 syntax. +Version `3.0.0` introduce Swift 3.0 syntax. If you are looking for the category implementation in Objective-C, make sure to checkout version `1.x` and prior, although the `2.x` is recomended. #Screenshot @@ -94,6 +94,15 @@ override func viewWillAppear(animated: Bool) { } ``` +##Followers +To move another view, like a toolbar, alongside the navigation bar you can provide the view or multiple views as the `followers` parameter: +```swift +if let navigationController = navigationController as? ScrollingNavigationController { + navigationController.followScrollView(tableView, delay: 50.0, followers: [toolbar]) +} +``` + + ##ScrollingNavigationControllerDelegate You can set a delegate to receive a call when the state of the navigation bar changes: ```swift @@ -149,12 +158,12 @@ controller.followScrollView(view, delay: 0, scrollSpeedFactor: 2) Check out the sample project for more details. #Author -[Andrea Mazzini](https://twitter.com/theandreamazz). I'm available for freelance work, feel free to contact me. +[Andrea Mazzini](https://twitter.com/theandreamazz). I'm available for freelance work, feel free to contact me. Want to support the development of [these free libraries](https://cocoapods.org/owners/734)? Buy me a coffee ☕️ via [Paypal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=46FNZD4PDVNRU). #Contributors -[Syo Ikeda](https://github.com/ikesyo) and [everyone](https://github.com/andreamazz/AMScrollingNavbar/graphs/contributors) kind enough to submit a pull request. +[Syo Ikeda](https://github.com/ikesyo) and [everyone](https://github.com/andreamazz/AMScrollingNavbar/graphs/contributors) kind enough to submit a pull request. #MIT License The MIT License (MIT) @@ -177,5 +186,3 @@ Want to support the development of [these free libraries](https://cocoapods.org/ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - - diff --git a/Source/ScrollingNavigationController.swift b/Source/ScrollingNavigationController.swift index d30430f0..54312974 100644 --- a/Source/ScrollingNavigationController.swift +++ b/Source/ScrollingNavigationController.swift @@ -73,6 +73,11 @@ open class ScrollingNavigationController: UINavigationController, UIGestureRecog */ open weak var scrollingNavbarDelegate: ScrollingNavigationControllerDelegate? + /** + An array of `UIView`s that will follow the navbar + */ + open var followers: [UIView] = [] + open fileprivate(set) var gestureRecognizer: UIPanGestureRecognizer? var delayDistance: CGFloat = 0 var maxDelay: CGFloat = 0 @@ -88,8 +93,9 @@ open class ScrollingNavigationController: UINavigationController, UIGestureRecog - parameter scrollableView: The view with the scrolling content that will be observed - parameter delay: The delay expressed in points that determines the scrolling resistance. Defaults to `0` - parameter scrollSpeedFactor : This factor determines the speed of the scrolling content toward the navigation bar animation + - parameter followers: An array of `UIView`s that will follow the navbar */ - open func followScrollView(_ scrollableView: UIView, delay: Double = 0, scrollSpeedFactor: Double = 1) { + open func followScrollView(_ scrollableView: UIView, delay: Double = 0, scrollSpeedFactor: Double = 1, followers: [UIView] = []) { self.scrollableView = scrollableView gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ScrollingNavigationController.handlePan(_:))) @@ -103,6 +109,7 @@ open class ScrollingNavigationController: UINavigationController, UIGestureRecog maxDelay = CGFloat(delay) delayDistance = CGFloat(delay) scrollingEnabled = true + self.followers = followers self.scrollSpeedFactor = CGFloat(scrollSpeedFactor) } @@ -306,6 +313,11 @@ open class ScrollingNavigationController: UINavigationController, UIGestureRecog updateSizing(scrollDelta) updateNavbarAlpha() restoreContentOffset(scrollDelta) + updateFollowers(scrollDelta) + } + + private func updateFollowers(_ delta: CGFloat) { + followers.forEach { $0.transform = $0.transform.translatedBy(x: 0, y: -delta) } } private func updateSizing(_ delta: CGFloat) { @@ -372,6 +384,7 @@ open class ScrollingNavigationController: UINavigationController, UIGestureRecog UIView.animate(withDuration: duration, delay: 0, options: UIViewAnimationOptions.beginFromCurrentState, animations: { self.updateSizing(delta) + self.updateFollowers(delta) self.updateNavbarAlpha() }, completion: nil) }