Skip to content

Instantly share code, notes, and snippets.

@Damagucci-Juice
Last active January 23, 2025 01:41
Show Gist options
  • Save Damagucci-Juice/3863e56455116f0b121f0908033f5303 to your computer and use it in GitHub Desktop.
Save Damagucci-Juice/3863e56455116f0b121f0908033f5303 to your computer and use it in GitHub Desktop.
import SwiftUI
struct ManualInlinView: View {
@State private var isInline = false
private let clearHeight = 200.0 // 이 부분을 수정 가능
var body: some View {
ScrollView {
VStack {
Color.blue
.frame(height: 300)
.background(
Color.clear
.frame(height: clearHeight)
.onVisibilityChange { isVisible in
isInline = !isVisible
}
)
.overlay {
Text("This area is a image")
.font(.title)
}
HStack {
Text("제목")
.font(.title)
Spacer()
}
.padding()
HStack {
Text("얄라리얄랴리얄라셩얄라리얄라얄라리얄랴리얄라셩얄라리얄라얄라리얄랴리얄라셩얄라리얄라얄라리얄랴리얄라셩얄라리얄라얄라리얄랴리얄라셩얄라리얄라")
}
.padding(.horizontal)
Color.gray
.frame(height: 400)
.overlay {
Text("This area is description")
.font(.title)
.foregroundStyle(.white)
}
Color.gray
.frame(height: 400)
.overlay {
Text("This area is description")
.font(.title)
.foregroundStyle(.white)
}
Color.gray
.frame(height: 400)
.overlay {
Text("This area is description")
.font(.title)
.foregroundStyle(.white)
}
}
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button {
isInline.toggle()
} label: {
Image(systemName: "chevron.left")
}
}
ToolbarItem(placement: .principal) {
if isInline {
Text("제목")
.bold()
}
}
}
.toolbarBackground(.tintColor, for: .navigationBar)
.toolbarBackground(
isInline ? .visible : .hidden,
for: .navigationBar
)
}
.ignoresSafeArea()
}
}
#Preview {
NavigationStack {
ManualInlinView()
}
}
struct ViewVisibilityModifier: ViewModifier {
let onChange: (Bool) -> Void
func body(content: Content) -> some View {
content
.background(
GeometryReader { geometry in
let minY = geometry.frame(in: .global).minY
Color.clear
.onChange(of: minY) { _ in
let isVisible = UIScreen.main.bounds.intersects(geometry.frame(in: .global))
onChange(isVisible)
}
}
)
}
}
extension View {
func onVisibilityChange(_ onChange: @escaping (Bool) -> Void) -> some View {
self.modifier(ViewVisibilityModifier(onChange: onChange))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment