Skip to content

Instantly share code, notes, and snippets.

@alextrob
Last active July 19, 2021 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alextrob/8d0e8a5767e6bed3b3921f7effbf32d6 to your computer and use it in GitHub Desktop.
Save alextrob/8d0e8a5767e6bed3b3921f7effbf32d6 to your computer and use it in GitHub Desktop.
import Foundation
import SwiftUI
extension ScrollView {
/// Workaround for large titles flickering on collapse. Not applied for iOS 15+.
/// Based on https://stackoverflow.com/a/67270977/99714 but avoids a questionable API by using a concrete type.
func largeTitleFlickerWorkaround() -> some View {
Group {
if #available(iOS 15.0, *) {
self
} else {
GeometryReader { geo in
WorkaroundScrollView(axes: axes, showsIndicators: showsIndicators, content: content.padding(geo.safeAreaInsets))
.edgesIgnoringSafeArea(.all)
}
}
}
}
private struct WorkaroundScrollView<ScrollViewContent: View>: View {
let axes: Axis.Set
let showsIndicators: Bool
let content: ScrollViewContent
var body: ScrollView<ScrollViewContent> {
ScrollView<ScrollViewContent>(axes, showsIndicators: showsIndicators, content: { content })
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible(), spacing: 16), GridItem(.flexible(), spacing: 16)], alignment: .center, spacing: 32, pinnedViews: []) {
ForEach(0..<10) { _ in
Text("Hello, world!")
}
}
}
.largeTitleFlickerWorkaround()
.navigationTitle("Scroll me")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment