Skip to content

Commit

Permalink
merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
asaake committed Mar 5, 2018
2 parents 0aae95c + 1b28193 commit d3854b1
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 61 deletions.
4 changes: 2 additions & 2 deletions HidingNavigationBar.podspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
Pod::Spec.new do |s|

s.name = "HidingNavigationBar"
s.version = "1.0.0"
s.version = "2.0.1"
s.license = { :type => "MIT", :file => "LICENSE" }
s.summary = "A swift library that manages hiding and showing a Navigation Bar as a user scrolls"
s.homepage = "https://github.com/tristanhimmelman/HidingNavigationBar"
s.author = { "Tristan Himmelman" => "[email protected]" }
s.source = { :git => 'https://github.com/tristanhimmelman/HidingNavigationBar.git', :tag => s.version.to_s }

s.ios.deployment_target = '8.0'
s.requires_arc = 'true'
s.requires_arc = true
s.source_files = 'HidingNavigationBar/**/*.swift'

end
55 changes: 38 additions & 17 deletions HidingNavigationBar/HidingNavigationBarManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
// Hiding navigation bar state
fileprivate var currentState = HidingNavigationBarState.Open
fileprivate var previousState = HidingNavigationBarState.Open

fileprivate var trackingScrollViewPanGestureRecognizer: UIPanGestureRecognizer!

fileprivate var panGesture: UIPanGestureRecognizer?

//Options
open var onForegroundAction = HidingNavigationForegroundAction.default

Expand Down Expand Up @@ -89,7 +88,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(HidingNavigationBarManager.handlePanGesture(_:)))
panGesture.delegate = self
scrollView.addGestureRecognizer(panGesture)
trackingScrollViewPanGestureRecognizer = panGesture
self.panGesture = panGesture

navBarController.expandedCenter = {[weak self] (view: UIView) -> CGPoint in
return CGPoint(x: view.bounds.midX, y: view.bounds.midY + (self?.statusBarHeight() ?? 0))
Expand All @@ -109,6 +108,9 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

deinit {
NotificationCenter.default.removeObserver(self)
if let panGesture = panGesture {
scrollView.removeGestureRecognizer(panGesture)
}
}

//MARK: Public methods
Expand Down Expand Up @@ -141,7 +143,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
}

open func viewWillAppear(_ animated: Bool) {
trackingScrollViewPanGestureRecognizer.isEnabled = true
panGesture?.isEnabled = true
expand()
}

Expand All @@ -151,15 +153,15 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

open func viewWillDisappear(_ animated: Bool) {
expand()
trackingScrollViewPanGestureRecognizer.isEnabled = false
panGesture?.isEnabled = false
}

open func updateValues() {
isUpdatingValues = true

var scrolledToTop = false

if scrollView.contentInset.top == -scrollView.contentOffset.y {
if scrollViewContentInset.top == -scrollView.contentOffset.y {
scrolledToTop = true
}

Expand All @@ -174,7 +176,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

if scrolledToTop {
var offset = scrollView.contentOffset
offset.y = -scrollView.contentInset.top
offset.y = -scrollViewContentInset.top
scrollView.contentOffset = offset
}

Expand Down Expand Up @@ -210,7 +212,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

//MARK: NSNotification

func applicationWillEnterForeground() {
@objc func applicationWillEnterForeground() {
switch onForegroundAction {
case .show:
_ = navBarController.expand()
Expand All @@ -221,6 +223,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
default:
break;
}
handleScrolling()
}

//MARK: Private methods
Expand All @@ -240,7 +243,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

fileprivate func shouldHandleScrolling() -> Bool {
// if scrolling down past top
if scrollView.contentOffset.y <= -scrollView.contentInset.top && currentState == .Open {
if scrollView.contentOffset.y <= -scrollViewContentInset.top && currentState == .Open {
return false
}

Expand All @@ -249,7 +252,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
return false
}

let scrollFrame = UIEdgeInsetsInsetRect(scrollView.bounds, scrollView.contentInset)
let scrollFrame = UIEdgeInsetsInsetRect(scrollView.bounds, scrollViewContentInset)
let scrollableAmount: CGFloat = scrollView.contentSize.height - scrollFrame.height
let scrollViewIsSuffecientlyLong: Bool = scrollableAmount > navBarController.totalHeight() * 3

Expand All @@ -272,13 +275,13 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
}

/* rounding to resolve a dumb issue with the contentOffset value */
let end = floor(scrollView.contentSize.height - scrollView.bounds.height + scrollView.contentInset.bottom - 0.5)
let end = floor(scrollView.contentSize.height - scrollView.bounds.height + scrollViewContentInset.bottom - 0.5)
if previousYOffset > end {
deltaY = max(0, deltaY - previousYOffset + end)
}

// 3 - Update contracting variable
if Float(fabs(deltaY)) > FLT_EPSILON {
if Float(fabs(deltaY)) > .ulpOfOne {
if deltaY < 0 {
currentState = .Contracting
} else {
Expand Down Expand Up @@ -340,13 +343,15 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
}

fileprivate func updateScrollContentInsetTop(_ top: CGFloat) {
let contentInset = UIEdgeInsets(top: top, left: scrollView.contentInset.top, bottom: scrollView.contentInset.left, right: scrollView.contentInset.right)
let top = adjustTopInset(top)

let contentInset = UIEdgeInsets(top: top, left: scrollViewContentInset.top, bottom: scrollViewContentInset.left, right: scrollViewContentInset.right)
if delegate?.hidingNavigationBarManagerShouldUpdateScrollViewInsets(self, insets: contentInset) == false {
return
}

if viewController.automaticallyAdjustsScrollViewInsets {
var contentInset = scrollView.contentInset
var contentInset = scrollViewContentInset
contentInset.top = top
scrollView.contentInset = contentInset
}
Expand Down Expand Up @@ -377,7 +382,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
var newContentOffset = scrollView.contentOffset
newContentOffset.y -= deltaY

let contentInset = scrollView.contentInset
let contentInset = scrollViewContentInset
let top = contentInset.top + deltaY

UIView.animate(withDuration: 0.2, animations: {
Expand All @@ -391,7 +396,7 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture

//MARK: Scroll handling

func handlePanGesture(_ gesture: UIPanGestureRecognizer){
@objc func handlePanGesture(_ gesture: UIPanGestureRecognizer){
switch gesture.state {
case .began:
topInset = navBarController.view.frame.size.height + extensionController.view.bounds.size.height + statusBarHeight()
Expand All @@ -410,4 +415,20 @@ open class HidingNavigationBarManager: NSObject, UIScrollViewDelegate, UIGesture
return true
}

//MARK: iOS 11 handling (adjustedContentInset, safeAreaInsets)

var scrollViewContentInset: UIEdgeInsets {
if #available(iOS 11.0, *) {
return scrollView.adjustedContentInset
} else {
return scrollView.contentInset
}
}

fileprivate func adjustTopInset(_ top: CGFloat) -> CGFloat {
if #available(iOS 11.0, *) {
return top - scrollView.safeAreaInsets.top // subtract safeAreaInsets for ios11
}
return top
}
}
10 changes: 5 additions & 5 deletions HidingNavigationBar/HidingViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class HidingViewController {
}

func isContracted() -> Bool {
return Float(fabs(view.center.y - contractedCenterValue().y)) < FLT_EPSILON
return Float(fabs(view.center.y - contractedCenterValue().y)) < .ulpOfOne
}

func isExpanded() -> Bool {
return Float(fabs(view.center.y - expandedCenterValue().y)) < FLT_EPSILON
return Float(fabs(view.center.y - expandedCenterValue().y)) < .ulpOfOne
}

func totalHeight() -> CGFloat {
Expand Down Expand Up @@ -90,7 +90,7 @@ class HidingViewController {

if alphaFadeEnabled {
var newAlpha: CGFloat = 1.0 - (expandedCenterValue().y - view.center.y) * 2 / contractionAmountValue()
newAlpha = CGFloat(min(max(FLT_EPSILON, Float(newAlpha)), 1.0))
newAlpha = CGFloat(min(max(.ulpOfOne, Float(newAlpha)), 1.0))

updateSubviewsToAlpha(newAlpha)
}
Expand All @@ -105,7 +105,7 @@ class HidingViewController {
return residual;
}

func snap(_ contract: Bool, completion:((Void) -> Void)!) -> CGFloat {
func snap(_ contract: Bool, completion:(() -> Void)!) -> CGFloat {
var deltaY: CGFloat = 0

UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions(), animations: {
Expand Down Expand Up @@ -170,7 +170,7 @@ class HidingViewController {
// loops through and subview and save the visible ones in navSubviews array
for subView in view.subviews {
let isBackgroundView = subView === view.subviews[0]
let isViewHidden = subView.isHidden || Float(subView.alpha) < FLT_EPSILON
let isViewHidden = subView.isHidden || Float(subView.alpha) < .ulpOfOne

if isBackgroundView == false && isViewHidden == false {
navSubviews?.append(subView)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
attributes = {
LastSwiftMigration = 0700;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0800;
LastUpgradeCheck = 0900;
ORGANIZATIONNAME = "Tristan Himmelman";
TargetAttributes = {
6A412A2A1BB1CBA4001C3F67 = {
Expand Down Expand Up @@ -439,7 +439,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.hearst.HidingNavigationBar;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -462,7 +462,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.hearst.HidingNavigationBar;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = "1,2";
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
Expand All @@ -477,14 +477,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -524,14 +530,20 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
Expand Down Expand Up @@ -565,7 +577,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tristanhimmelman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -578,7 +590,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tristanhimmelman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -595,7 +607,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tristanhimmelman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HidingNavigationBarSample.app/HidingNavigationBarSample";
};
name = Debug;
Expand All @@ -609,7 +621,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tristanhimmelman.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/HidingNavigationBarSample.app/HidingNavigationBarSample";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0800"
LastUpgradeVersion = "0900"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -36,6 +37,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HidingNavTabViewController: UIViewController, UITableViewDataSource, UITab
hidingNavBarManager?.viewWillDisappear(animated)
}

func cancelButtonTouched(){
@objc func cancelButtonTouched(){
navigationController?.dismiss(animated: true, completion: nil)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MasterViewController: UITableViewController {

func styleNavigationController(_ navigationController: UINavigationController){
navigationController.navigationBar.isTranslucent = true
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
navigationController.navigationBar.tintColor = UIColor.white
navigationController.navigationBar.barTintColor = UIColor(red: 41/255, green: 141/255, blue: 250/255, alpha: 1)
}
Expand Down
Loading

0 comments on commit d3854b1

Please sign in to comment.