-
-
Save Damagucci-Juice/3863e56455116f0b121f0908033f5303 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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