Aligning HStack without equal sizing in SwiftUI - ios

I have a custom chart with some texts as seen below. It aligns the first text correctly, it aligns the last text incorrectly. I need the VStack with the texts to stretch like an accordion so that the last text's centerY aligns with the bottom of the chart.
As seen with the blue Xcode highlight, the VStack receives the same size as the chart with an offset.
To align the first text, I extended VerticalAlignment as seen in WWDC 2019 - Session 237.
extension VerticalAlignment {
private enum TopChartAndMidTitle: AlignmentID {
static func defaultValue(in dimensions: ViewDimensions) -> Length {
return dimensions[.top]
}
}
static let topChartAndMidTitle = VerticalAlignment(TopChartAndMidTitle.self)
}
Then I used it like this:
var labels = ["1900", "1800", "1700", "1600", "1500", "1400"]
var body: some View {
HStack(alignment: .topChartAndMidTitle) {
chart()
.alignmentGuide(.topChartAndMidTitle) { $0[.top] }
valueLabels()
}
}
private func valueLabels() -> AnyView {
AnyView(VStack {
Text(labels[0]).font(.footnote)
.alignmentGuide(.topChartAndMidTitle) { $0[.bottom] / 2 }
ForEach(labels.dropFirst().identified(by: \.self)) { label in
Spacer()
Text(label).font(.footnote)
}
})
}
Question: How do I align the last text against the chart bottom and thereby stretch the VStack?
To test this, just replace my custom chart with Rectangle().fill(Color.red). It will result in the same problem.

Today I would do it with the following approach...
Result demo
Code (TopChartAndMidTitle used from original question "as is")
struct TestAlignScales: View {
var labels = ["1900", "1800", "1700", "1600", "1500", "1400"]
#State private var graphHeight = CGFloat.zero
var body: some View {
HStack(alignment: .topChartAndMidTitle) {
Rectangle().fill(Color.red)
.alignmentGuide(.topChartAndMidTitle) { d in
if self.graphHeight != d.height {
self.graphHeight = d.height
}
return d[.top]
}
valueLabels()
}
}
#State private var delta = CGFloat.zero
private func valueLabels() -> some View {
VStack {
ForEach(labels, id: \.self) { label in
VStack {
if label == self.labels.first! {
Text(label).font(.footnote)
.alignmentGuide(.topChartAndMidTitle) { d in
if self.delta != d.height {
self.delta = d.height
}
return d[VerticalAlignment.center]
}
} else {
Text(label).font(.footnote)
}
if label != self.labels.last! {
Spacer()
}
}
}
}
.frame(height: graphHeight + delta)
}
}
struct TestAlignScales_Previews: PreviewProvider {
static var previews: some View {
TestAlignScales()
.frame(height: 400)
}
}

Related

SwiftUI menu button is slow to resize when title string changes length

I have the following SwiftUI menu:
import SwiftUI
struct ContentView: View {
private enum Constants {
static let arrowIcon = Image(systemName: "chevron.down")
static let background = Color.blue
static let buttonFont = Font.system(size: 14.0)
}
#State private var menuIndex = 0
private let menuItems = ["This year", "Last Year"]
private var menuTitle: String {
guard menuItems.indices.contains(menuIndex) else { return "" }
return menuItems[menuIndex]
}
// MARK: - Views
var body: some View {
makeMenu()
}
// MARK: - Buttons
private func menuItemTapped(title: String) {
guard let index = menuItems.firstIndex(of: title) else { return }
menuIndex = index
}
// MARK: - Factory
#ViewBuilder private func makeMenu() -> some View {
Menu {
ForEach(menuItems, id: \.self) { title in
Button(title, action: { menuItemTapped(title: title) })
}
} label: {
Text("\(menuTitle) \(Constants.arrowIcon)")
.font(Constants.buttonFont)
.fontWeight(.bold)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
When a menu item is tapped the index to the title array is updated causing the Text title to update. This works as desired however the text is slow to resize to the changes Last Year... will briefly show before it resizes correctly.
What am I doing wrong here?
This is because the size of the label is being set but not being changed. If you're okay with it, setting a maxWidth of .infinity on the frame of the Label gives you this capability.
#ViewBuilder private func makeMenu() -> some View {
VStack {
Menu {
ForEach(menuItems, id: \.self) { title in
Button(title, action: { menuItemTapped(title: title) })
}
} label: {
Text("\(menuTitle) \(Constants.arrowIcon)")
.font(Constants.buttonFont)
.fontWeight(.bold)
.frame(maxWidth: .infinity) //--> Add this line
}
}
}

How to scroll through items in scroll view using keyboard arrows in SwiftUI?

I've built a view that has scroll view of horizontal type with HStack for macOS app. Is there a way to circle those items using keyboard arrows?
(I see that ListView has a default behavior but for other custom view types there are none)
click here to see the screenshot
var body: some View {
VStack {
ScrollView(.horizontal, {
HStack {
ForEach(items.indices, id: \.self) { index in
//custom view for default state and highlighted state
}
}
}
}
}
any help is appreciated :)
You could try this example code, using my previous post approach, but with a horizontal scrollview instead of a list. You will have to adjust the code to your particular app. My approach consists only of a few lines of code that monitors the key events.
import Foundation
import SwiftUI
import AppKit
struct ContentView: View {
let fruits = ["apples", "pears", "bananas", "apricot", "oranges"]
#State var selection: Int = 0
#State var keyMonitor: Any?
var body: some View {
ScrollView(.horizontal) {
HStack(alignment: .center, spacing: 0) {
ForEach(fruits.indices, id: \.self) { index in
VStack {
Image(systemName: "globe")
.resizable()
.scaledToFit()
.frame(width: 20, height: 20)
.padding(10)
Text(fruits[index]).tag(index)
}
.background(selection == index ? Color.red : Color.clear)
.padding(10)
}
}
}
.onAppear {
keyMonitor = NSEvent.addLocalMonitorForEvents(matching: [.keyDown]) { nsevent in
if nsevent.keyCode == 124 { // arrow right
selection = selection < fruits.count ? selection + 1 : 0
} else {
if nsevent.keyCode == 123 { // arrow left
selection = selection > 1 ? selection - 1 : 0
}
}
return nsevent
}
}
.onDisappear {
if keyMonitor != nil {
NSEvent.removeMonitor(keyMonitor!)
keyMonitor = nil
}
}
}
}
Approach I used
Uses keyboard shortcuts on a button
Alternate approach
To use commands (How to detect keyboard events in SwiftUI on macOS?)
Code:
Model
struct Item: Identifiable {
var id: Int
var name: String
}
class Model: ObservableObject {
#Published var items = (0..<100).map { Item(id: $0, name: "Item \($0)")}
}
Content
struct ContentView: View {
#StateObject private var model = Model()
#State private var selectedItemID: Int?
var body: some View {
VStack {
Button("move right") {
moveRight()
}
.keyboardShortcut(KeyEquivalent.rightArrow, modifiers: [])
ScrollView(.horizontal) {
LazyHGrid(rows: [GridItem(.fixed(180))]) {
ForEach(model.items) { item in
ItemCell(
item: item,
isSelected: item.id == selectedItemID
)
.onTapGesture {
selectedItemID = item.id
}
}
}
}
}
}
private func moveRight() {
if let selectedItemID {
if selectedItemID + 1 >= model.items.count {
self.selectedItemID = model.items.last?.id
} else {
self.selectedItemID = selectedItemID + 1
}
} else {
selectedItemID = model.items.first?.id
}
}
}
Cell
struct ItemCell: View {
let item: Item
let isSelected: Bool
var body: some View {
ZStack {
Rectangle()
.foregroundColor(isSelected ? .yellow : .blue)
Text(item.name)
}
}
}

How to prevent ScrollView to automatically show items added on top of inner ForEach

I'm building a bidirectional scrolling list of items and we need to add items to the head of the array, but the ScrollView is automatically scrolling down (probably to preserve the contentOffset?) and I can't achieve what I want, which would be that the items are ready to be displayed but only if the user scrolls up.
I'm not sure if there is a way to "fix" this by fiddling with ScrollView or if wrapping UIScrollView is just the easier way for now.
Snippet to reproduce:
struct ContentView: View {
#State var list = [1, 2, 3]
var body: some View {
VStack {
ScrollView {
LazyVStack {
ForEach(Array(zip(list.indices, list)), id: \.0) { index, element in
Text("\(element)")
}
}
}
.frame(maxHeight: 300)
Button(action: {
list = [Int.random(in: 1...1000)] + list
}) {
Text("Add")
.font(.title)
}
}
}
}
We can try this :
We need to read the offset of ScrollView's content. We'll use PreferenceKey.
private struct Offset: PreferenceKey {
static var defaultValue: CGPoint = .zero
static func reduce(value: inout CGPoint, nextValue: () -> CGPoint) {}
}
We need to store the new elements (when user taps on "Add") until they are added (when user scrolls to the top) :
struct MutableScrollView: View {
#State private var list = Array(1 ... 100)
#State private var elementsToAdd = Array<Int>()
#State private var onTop: Bool = true
onClick is called when the user taps the Button. offsetChanged when we scroll.
func addNewElements() {
if onTop {
list = elementsToAdd + list
elementsToAdd.removeAll()
}
}
func onClick() {
elementsToAdd.append(Int.random(in: 100 ... 1000))
addNewElements()
}
func offsetChanged(_ offset: CGPoint) {
onTop = offset.y >= 0
addNewElements()
}
The View :
var body: some View {
VStack {
ScrollView {
GeometryReader { geometry in
Color.clear.preference(
key: Offset.self,
value: geometry.frame(in: .named("scrollView")).origin
)
}.frame(width: 0, height: 0)
LazyVStack {
ForEach(Array(zip(list.indices, list)), id: \.1) { _, element in
Text("\(element)")
}
}
}
.frame(maxHeight: 300)
.coordinateSpace(name: "scrollView")
.onPreferenceChange(Offset.self) { offsetChanged($0) }
Button(action: onClick) {
Text("Add")
.font(.title)
}
}
}
}

SwiftUI - Hide custom onDelete View on tap gesture

I have LazyVStack view that contains a list of views. Each one of the views has a different color and there is 8 points space between them. Threrefore, I can not use List.
So I am trying to build a custom trailing swipe that functions similar to the onDelete method of List. This is my code and it is not perfect, but I am on the right directin, I think.
Test Data - List of countries
class Data: ObservableObject {
#Published var countries: [String]
init() {
self.countries = NSLocale.isoCountryCodes.map { (code:String) -> String in
let id = NSLocale.localeIdentifier(fromComponents: [NSLocale.Key.countryCode.rawValue: code])
return NSLocale(localeIdentifier: "en_US").displayName(forKey: NSLocale.Key.identifier, value: id) ?? "Country not found for code: \(code)"
}
}
}
ContentView
struct ContentView: View {
#ObservedObject var data: Data = Data()
var body: some View {
ScrollView {
LazyVStack {
ForEach(data.countries, id: \.self) { country in
VStack {
SwipeView(content: {
VStack(spacing: 0) {
Spacer()
Text(country)
.frame(minWidth: 0, maxWidth: .infinity)
Spacer()
}
.background(Color.yellow)
}, trailingActionView: {
Image(systemName: "trash")
.foregroundColor(.white)
}) {
self.data.countries.removeAll {$0 == country}
}
}
.clipShape(Rectangle())
}
}
}
.padding(.vertical, 16)
}
}
Custom SwipeView
struct SwipeView<Content: View, TrailingActionView: View>: View {
let width = UIScreen.main.bounds.width - 32
#State private var height: CGFloat = .zero
#State var offset: CGFloat = 0
let content: Content
let trailingActionView: TrailingActionView
var onDelete: () -> ()
init(#ViewBuilder content: () -> Content,
#ViewBuilder trailingActionView: () -> TrailingActionView,
onDelete: #escaping () -> Void) {
self.content = content()
self.trailingActionView = trailingActionView()
self.onDelete = onDelete
}
var body: some View {
ZStack {
HStack(spacing: 0) {
Button(action: {
withAnimation {
self.onDelete()
}
}) {
trailingActionView
}
.frame(minHeight: 0, maxHeight: .infinity)
.frame(width: 60)
Spacer()
}
.background(Color.red)
.frame(width: width)
.offset(x: width + self.offset)
content
.frame(width: width)
.contentShape(Rectangle())
.offset(x: self.offset)
.gesture(DragGesture().onChanged(onChanged).onEnded { value in
onEnded(value: value, width: width)
})
}
.background(Color.white)
}
private func onChanged(value: DragGesture.Value) {
let translation = value.translation.width
if translation < 0 {
self.offset = translation
} else {
}
}
private func onEnded(value: DragGesture.Value,width: CGFloat) {
withAnimation(.easeInOut) {
let translation = -value.translation.width
if translation > width - 16 {
self.onDelete()
self.offset = -(width * 2)
}
else if translation > 50 {
self.offset = -50
}
else {
self.offset = 0
}
}
}
}
It has one annoying problem: If you swipe a row and do not delete it. And if you swipe another views, they don not reset. All the trailing Delete Views are visible. But I want to reset/ swipe back if you tap anywhere outside the Delete View.
I want to swipe back if you tap anywhere outside the Delete View. So how to do it?
First off, to know which cell is swiped the SwipeViews needs an id. If you don't want to set them from external I guess this will do:
struct SwipeView<Content: View, TrailingActionView: View>: View {
...
#State var id = UUID()
...
}
Then you need to track which cell is swiped, the SwiftUI way of relaying data to siblings is by a Binding that is saved in it's parent. Read up on how to pass data around SwiftUI Views. If you want to be lazy you can also just have a static object that saves the selected cell:
class SwipeViewHelper: ObservableObject {
#Published var swipedCell: UUID?
private init() {}
static var shared = SwipeViewHelper()
}
struct SwipeView<Content: View, TrailingActionView: View>: View {
...
#ObservedObject var helper = SwipeViewHelper.shared
...
}
Then you have to update the swipedCell. We want the cell to close when we START swiping on a different cell:
private func onChanged(value: DragGesture.Value) {
...
if helper.swipedCell != nil {
helper.swipedCell = nil
}
...
}
And when a cell is open we save it:
private func onEnded(value: DragGesture.Value,width: CGFloat) {
withAnimation(.easeInOut) {
...
else if translation > 50 {
self.offset = -50
helper.swipedCell = id
}
...
}
}
Then we have to respond to changes of the swipedCell. We can do that by adding an onChange inside the body of SwipeView:
.onChange(of: helper.swipedCell, perform: { newCell in
if newCell != id {
withAnimation(.easeInOut) {
self.offset = 0
}
}
})
Working gist: https://gist.github.com/Amzd/61a957a1c5558487f6cc5d3ce29cf508

SwiftUI: Make ScrollView scrollable only if it exceeds the height of the screen

Currently I have a view that looks like this.
struct StatsView: View {
var body: some View {
ScrollView {
Text("Test1")
Text("Test2")
Text("Test3")
}
}
}
This renders a view that contains 3 texts inside a scroll view, whenever I drag any of these texts in the screen the view will move cause its scrollable, even if these 3 texts fit in the screen and there is remaining space. What I want to achieve is to only make the ScrollView scrollable if its content exceeds the screen height size, if not, I want the view to be static and don't move. I've tried using GeometryReader and setting the scrollview frame to the screen width and height, also the same for the content but I continue to have the same behaviour, also I have tried setting the minHeight, maxHeight without any luck.
How can I achieve this?
For some reason I could not make work any of the above, but it did inspire me find a solution that did in my case. It's not as flexible as others, but could easily be adapted to support both axes of scrolling.
import SwiftUI
struct OverflowContentViewModifier: ViewModifier {
#State private var contentOverflow: Bool = false
func body(content: Content) -> some View {
GeometryReader { geometry in
content
.background(
GeometryReader { contentGeometry in
Color.clear.onAppear {
contentOverflow = contentGeometry.size.height > geometry.size.height
}
}
)
.wrappedInScrollView(when: contentOverflow)
}
}
}
extension View {
#ViewBuilder
func wrappedInScrollView(when condition: Bool) -> some View {
if condition {
ScrollView {
self
}
} else {
self
}
}
}
extension View {
func scrollOnOverflow() -> some View {
modifier(OverflowContentViewModifier())
}
}
Usage
VStack {
// Your content
}
.scrollOnOverflow()
Here is a possible approach if a content of scroll view does not require user interaction (as in PO question):
Tested with Xcode 11.4 / iOS 13.4
struct StatsView: View {
#State private var fitInScreen = false
var body: some View {
GeometryReader { gp in
ScrollView {
VStack { // container to calculate total height
Text("Test1")
Text("Test2")
Text("Test3")
//ForEach(0..<50) { _ in Text("Test") } // uncomment for test
}
.background(GeometryReader {
// calculate height by consumed background and store in
// view preference
Color.clear.preference(key: ViewHeightKey.self,
value: $0.frame(in: .local).size.height) })
}
.onPreferenceChange(ViewHeightKey.self) {
self.fitInScreen = $0 < gp.size.height // << here !!
}
.disabled(self.fitInScreen)
}
}
}
Note: ViewHeightKey preference key is taken from this my solution
My solution does not disable content interactivity
struct ScrollViewIfNeeded<Content: View>: View {
#ViewBuilder let content: () -> Content
#State private var scrollViewSize: CGSize = .zero
#State private var contentSize: CGSize = .zero
var body: some View {
ScrollView(shouldScroll ? [.vertical] : []) {
content().readSize($contentSize)
}
.readSize($scrollViewSize)
}
private var shouldScroll: Bool {
scrollViewSize.height <= contentSize.height
}
}
struct SizeReaderModifier: ViewModifier {
#Binding var size: CGSize
func body(content: Content) -> some View {
content.background(
GeometryReader { geometry -> Color in
DispatchQueue.main.async {
size = geometry.size
}
return Color.clear
}
)
}
}
extension View {
func readSize(_ size: Binding<CGSize>) -> some View {
self.modifier(SizeReaderModifier(size: size))
}
}
Usage:
struct StatsView: View {
var body: some View {
ScrollViewIfNeeded {
Text("Test1")
Text("Test2")
Text("Test3")
}
}
}
I've made a more comprehensive component for this problem, that works with all type of axis sets:
Code
struct OverflowScrollView<Content>: View where Content : View {
#State private var axes: Axis.Set
private let showsIndicator: Bool
private let content: Content
init(_ axes: Axis.Set = .vertical, showsIndicators: Bool = true, #ViewBuilder content: #escaping () -> Content) {
self._axes = .init(wrappedValue: axes)
self.showsIndicator = showsIndicators
self.content = content()
}
fileprivate init(scrollView: ScrollView<Content>) {
self._axes = .init(wrappedValue: scrollView.axes)
self.showsIndicator = scrollView.showsIndicators
self.content = scrollView.content
}
public var body: some View {
GeometryReader { geometry in
ScrollView(axes, showsIndicators: showsIndicator) {
content
.background(ContentSizeReader())
.onPreferenceChange(ContentSizeKey.self) {
if $0.height <= geometry.size.height {
axes.remove(.vertical)
}
if $0.width <= geometry.size.width {
axes.remove(.horizontal)
}
}
}
}
}
}
private struct ContentSizeReader: View {
var body: some View {
GeometryReader {
Color.clear
.preference(
key: ContentSizeKey.self,
value: $0.frame(in: .local).size
)
}
}
}
private struct ContentSizeKey: PreferenceKey {
static var defaultValue: CGSize { .zero }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = CGSize(width: value.width+nextValue().width,
height: value.height+nextValue().height)
}
}
// MARK: - Implementation
extension ScrollView {
public func scrollOnlyOnOverflow() -> some View {
OverflowScrollView(scrollView: self)
}
}
Usage
ScrollView([.vertical, .horizontal]) {
Text("Ciao")
}
.scrollOnlyOnOverflow()
Attention
This code could not work in those situations:
Content size change dynamically
ScrollView size change dynamically
Device orientation change
Building on Asperi's answer, we can conditionally wrap the view with a ScrollView when we know the content is going to overflow. This is an extension to View you can create:
extension View {
func useScrollView(
when condition: Bool,
showsIndicators: Bool = true
) -> AnyView {
if condition {
return AnyView(
ScrollView(showsIndicators: showsIndicators) {
self
}
)
} else {
return AnyView(self)
}
}
}
and in the main view, just check if the view is too long using your logic, perhaps with GeometryReader and the background color trick:
struct StatsView: View {
var body: some View {
VStack {
Text("Test1")
Text("Test2")
Text("Test3")
}
.useScrollView(when: <an expression you write to decide if the view fits, maybe using GeometryReader>)
}
}
}
I can't comment, because I don't have enough reputation, but I wanted to add a comment in the happymacaron answer. The extension worked for me perfectly, and for the Boolean to show or not the scrollView, I used the this code to know the height of the device:
///Device screen
var screenDontFitInDevice: Bool {
UIScreen.main.bounds.size.height < 700 ? true : false
}
So, with this var I can tell if the device height is less than 700, and if its true I want to make the view scrollable so the content can show without any problem.
So wen applying the extension I just do this:
struct ForgotPasswordView: View {
var body: some View {
VStack {
Text("Scrollable == \(viewModel.screenDontFitInDevice)")
}
.useScrollView(when: viewModel.screenDontFitInDevice, showsIndicators: false)
}
}
The following solution allows you to use Button inside:
Based on #Asperi solution
SpecialScrollView:
/// Scrollview disabled if smaller then content view
public struct SpecialScrollView<Content> : View where Content : View {
let content: Content
#State private var fitInScreen = false
public init(#ViewBuilder content: () -> Content) {
self.content = content()
}
public var body: some View {
if fitInScreen == true {
ZStack (alignment: .topLeading) {
content
.background(GeometryReader {
Color.clear.preference(key: SpecialViewHeightKey.self,
value: $0.frame(in: .local).size.height)})
.fixedSize()
Rectangle()
.foregroundColor(.clear)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
}
}
else {
GeometryReader { gp in
ScrollView {
content
.background(GeometryReader {
Color.clear.preference(key: SpecialViewHeightKey.self,
value: $0.frame(in: .local).size.height)})
}
.onPreferenceChange(SpecialViewHeightKey.self) {
self.fitInScreen = $0 < gp.size.height
}
}
}
}
}
struct SpecialViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = value + nextValue()
}
}
USE:
struct SwiftUIView6: View {
#State private var fitInScreen = false
var body: some View {
VStack {
Text("\(fitInScreen ? "true":"false")")
SpecialScrollView {
ExtractedView()
}
}
}
}
struct SwiftUIView6_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView6()
}
}
struct ExtractedView: View {
#State var text:String = "Text"
var body: some View {
VStack { // container to calculate total height
Text(text)
.onTapGesture {text = text == "TextModified" ? "Text":"TextModified"}
Text(text)
.onTapGesture {text = text == "TextModified" ? "Text":"TextModified"}
Text(text)
.onTapGesture {text = text == "TextModified" ? "Text":"TextModified"}
Spacer()
//ForEach(0..<50) { _ in Text(text).onTapGesture {text = text == "TextModified" ? "Text":"TextModified"} } // uncomment for test
}
}
}
According to the Asperi! answer, I created a custom component that covers reported issue
private struct ViewHeightKey: PreferenceKey {
static var defaultValue: CGFloat { 0 }
static func reduce(value: inout Value, nextValue: () -> Value) {
value = value + nextValue()
}
}
struct SmartScrollView<Content: View>: View {
#State private var fitInScreen = false
#State var axes = Axis.Set.vertical
let content: () -> Content
var body: some View {
GeometryReader { gp in
ScrollView(axes) {
content()
.onAppear {
axes = fitInScreen ? [] : .vertical
}
.background(GeometryReader {
// calculate height by consumed background and store in
// view preference
Color.clear.preference(key: ViewHeightKey.self,
value: $0.frame(in: .local).size.height) })
}
.onPreferenceChange(ViewHeightKey.self) {
self.fitInScreen = $0 < gp.size.height // << here !!
}
}
}
}
usage:
var body: some View {
SmartScrollView {
Content()
}
}
Unfourtunatly none of the solutions here allow for dynamically responding to when turning on accessibility and increasing the font size on the fly. Hoping there will be a complete solution without disabling the UI within the scrollView.
This might help in case you need to listen on changes in font sizes, context changes etc. Simply just change the viewIndex to you needed identifier for changes.
This view will inform you about if it's scrolled or not, and also if the original content fits inside the scrollview or if it's scrollable.
Hope it helps someone :)
import Combine
import SwiftUI
struct FeedbackScrollView<Content: View>: View {
/// Used to inform the FeedbackScrollView if the view changes (mainly used in 'flows')
var viewIndex: Double
/// Notifies if the scrollview is scrolled
#Binding var scrollViewIsScrolled: Bool
/// Notifies if the scrollview has overflow in it's content, to indicate if it can scroll or now
#Binding var scrollViewCanScroll: Bool
/// The content you want to put into the scrollview.
#ViewBuilder private let content: () -> Content
public init(
viewIndex: Double = 0,
scrollViewIsScrolled: Binding<Bool> = .constant(false),
scrollViewCanScroll: Binding<Bool>,
#ViewBuilder content: #escaping () -> Content
) {
self.viewIndex = viewIndex
self._scrollViewIsScrolled = scrollViewIsScrolled
self._scrollViewCanScroll = scrollViewCanScroll
self.content = content
}
var body: some View {
GeometryReader { geometry in
ScrollView {
offsetReader
content()
.frame(
minHeight: geometry.size.height,
alignment: .topLeading
)
.background(
GeometryReader { contentGeometry in
Color.clear
.onAppear {
scrollViewCanScroll = contentGeometry.size.height > geometry.size.height
}
.onChange(of: viewIndex) { _ in
scrollViewCanScroll = contentGeometry.size.height > geometry.size.height
}
}
)
}
.dismissKeyboardOnDrag()
.coordinateSpace(name: "scrollSpace")
.onPreferenceChange(OffsetPreferenceKey.self, perform: offsetChanged(offset:))
}
}
var offsetReader: some View {
GeometryReader { proxy in
Color.clear
.preference(
key: OffsetPreferenceKey.self,
value: proxy.frame(in: .named("scrollSpace")).minY
)
}
.frame(height: 0)
}
private func offsetChanged(offset: CGFloat) {
withAnimation {
scrollViewIsScrolled = offset < 0
}
}
}
private struct OffsetPreferenceKey: PreferenceKey {
static var defaultValue: CGFloat = .zero
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {}
}
struct FeedbackScrollView_Previews: PreviewProvider {
static var previews: some View {
FeedbackScrollView(
viewIndex: 0,
scrollViewIsScrolled: .constant(false),
scrollViewCanScroll: .constant(true)
) { }
}
}
Use it like this:
...
#State var scrollViewIsScrolled: Bool
#State var scrollViewCanScroll: Bool
FeedbackScrollView(
viewIndex: numberOfCompletedSteps,
scrollViewIsScrolled: $scrollViewIsScrolled,
scrollViewCanScroll: $scrollViewCanScroll
) {
// Your (scrollable) content goes here..
}

Resources