Select UIBarButtonItem
s in Navigation Bars by Scrolling Up.
No need for Reachability or awkwardly holding your phone to reach a button in the top corner.
import ScrollSelection
var scrollSelection: ScrollSelection!
override func viewDidLoad() {
super.viewDidLoad()
scrollSelection = createScrollSelection()
scrollView.delegate = self
// If you are using tableViews, use `tableView.delegate = self`
// If you are using textViews, use `textView.delegate = self`
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollSelection.didScroll()
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollSelection.didEndDragging()
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollSelection.didEndDecelerating()
}
// Add a search bar to scrollSelection
scrollSelection.selectionSequence = [.searchBar(mySearchBar),
// Add a custom button
.button(myButton),
// Default, right and left bar buttons
.rightBarButtons,
.leftBarButtons]
Note: For updated documentation information, make use of the Quick Help section (or ⌥-click the declaration)
Create Scroll Selection
Set-Up Scroll Selection on this View Controller
func createScrollSelection(withOffset offsetMultiplier: CGFloat = 70,
usingStyle style: [ScrollSelection.Style] = ScrollSelection.Style.defaultStyle) -> ScrollSelection
withOffset offsetMultiplier
- Distance between each button selection
- Default Value:
70
usingStyle style
- Scroll Selection Style. Use
ScrollSelection.Style.defaultStyle
for default implementation or remove this parameter - Default Value:
ScrollSelection.Style.defaultStyle
- Refer to Style for the various style information
- Scroll Selection Style. Use
An instance of Scroll Selection that is already set up
In your viewDidLoad
function,
override func viewDidLoad() {
super.viewDidLoad()
// Default implementation
scrollSelection = createScrollSelection()
// Custom implementation
scrollSelection = createScrollSelection(withOffset: 70, usingStyle: ScrollSelection.Style.defaultStyle)
}
Update Bar Buttons
Update bar buttons with Scroll Selection
func updateBarButtons(barButtonSide direction: ScrollSelection.Direction = .all)
Call this function whenever a change is made to the navigation bar buttons
barButtonSide direction
.left
corresponds to the left bar buttons,.right
corresponds to the right bar buttons,.all
updates all buttons.- Default Value: .all
- Refer to Direction for the various direction information
After updating left bar button items,
scrollSelection.updateBarButtons(barButtonSide: .left)
Did Scroll
Update ScrollSelection when the scrollview scrolls
func didScroll()
Updates scroll selection by highlighting or removing highlights on corresponding buttons
To be called in scrollViewDidScroll
function that is part of UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollSelection.didScroll()
}
}
Did End Dragging
Update ScrollSelection when user stops dragging scrollView
func didEndDragging()
Called when scrollView is released (ends dragging) and thus, scroll selection will select the corresponding bar button
To be called in scrollViewDidEndDragging
function that is part of UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollSelection.didEndDragging()
}
}
Did End Decelerating
Update ScrollSelection once the scrollView stops decelerating
func didEndDecelerating()
Called when scrollView is ends deceerating and thus, scroll selection will reset to original state
To be called in scrollViewDidEndDecelerating
function that is part of UIScrollViewDelegate
extension ViewController: UIScrollViewDelegate {
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollSelection.didEndDecelerating()
}
}
Offset Multiplier
Y-Axis offset between selecting buttons
var offsetMultiplier: CGFloat!
Should be automatically set in by init
or the UIViewController Implementation
Scroll View
Target UIScrollView for Scroll Selection
var scrollView: UIScrollView?
Should be automatically set in by init
or the UIViewController Implementation
Scroll Selection will ignore all scrollViews except for the targetted one.
scrollSelection.scrollView = myScrollView
Haptic Style
Haptic feedback styles
var hapticStyle: HapticsStyle = .variableIncreasing
It uses .variableIncreasing
as default value.
Refer to HapticsStyle for the various styles
scrollSelection.hapticStyle = .variableIncreasing
Style
Current scroll selection style
var style: [Style]!
Should be automatically set in by init
or the UIViewController Implementation.
Refer to Scroll Selection Styles for the various styles
// Using the default style
scrollSelection.style = ScrollSelection.Style.defaultStyle
// Using a custom style
scrollSelection.style = [.circularHighlight(using: .systemRed, expands: true)]
Highlight
Changes the Button tint color during Scroll Selection
public static func highlight(using color: UIColor = UIColor.systemBlue.withAlphaComponent(0.7)) -> Style
using color
- Color to change to
- Default Value:
.systemBlue
with alpha of 0.7
A scroll selection style
Circular Highlight
Adds a circular highlight/background to the button that is being selected
public static func circularHighlight(using color: UIColor = .systemGray4,
expands: Bool = true,
fades: Bool = true) -> Style
using color
- Color of highlight
- Default Value:
.systemGray4
with alpha of 0.7
expands
- If true, circular highlights will expand radially to show emphasis on the button as the user scrolls up. Otherwise, it will stay static and the highlight will not expand.
fades
- If true, circular highlight background will fade as the user scrolls up. Otherwise, it will jump from one to another, without fading.
A scroll selection style
Normal
Normal Haptic Style
case normal
Normal corresponds to UISelectionFeedbackGenerator().selectionChanged()
. A more subtle haptic style.
Variable Increasing
Default style, feedback becomes more pronounced as user scrolls up
case variableIncreasing
First Button -> Last Button
Weak -> Strong
Variable Decreasing
Haptic feedback becomes less pronounced as user scrolls up
case variableDecreasing
First Button -> Last Button
Strong -> Weak
Left
Update Left Bar Buttons
public static let left: Direction = Direction(rawValue: 1 << 0)
Right
Update Right Bar Buttons
public static let right: Direction = Direction(rawValue: 1 << 1)
All
Update Both Left and Right Bar Buttons
public static let all: Direction = [.left, .right]