struct NavigationBarProxy: UIViewControllerRepresentable {
var callback: (UIView, UINavigationBar) -> Void
private let proxyController = ViewController()
func makeUIViewController(context: Context) -> UIViewController {
proxyController.callback = callback
return proxyController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
typealias UIViewControllerType = UIViewController
private class ViewController: UIViewController {
var callback: (UIView, UINavigationBar) -> Void = { _, _ in }
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navigationController = self.navigationController {
self.callback(navigationController.view, navigationController.navigationBar)
}
}
}
}
[사용방법]
struct ContentView: View {
var body: some View {
NavigationView {
ZStack {
Color.yellow.ignoresSafeArea(.all, edges: .all)
VStack {
Text("ContentView")
.padding()
.font(.title)
.foregroundColor(.white)
}
}
.navigationTitle("Side Menu Demo")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(action: {
self.showMenu.toggle()
},
label: {
Image(systemName: "text.justify")
.font(.title3)
.foregroundColor(.red)
})
}
}
.background(NavigationBarProxy(callback: { view, navBar in
print("""
navbar \(navBar.bounds)
view-inset \(view.safeAreaInsets)
""")
}))
}
}
}
[결과]
navbar (0.0, 0.0, 390.0, 44.0)
view-inset UIEdgeInsets(top: 47.0, left: 0.0, bottom: 34.0, right: 0.0)
'SwiftUI' 카테고리의 다른 글
[SwiftUI] 초간단 사이드 메뉴 UI (0) | 2024.06.27 |
---|---|
[SwiftUI] Gradient Tag Toggle Button (0) | 2024.06.11 |
[SwiftUI] Binding 변수가 옵셔널인 경우 unwrapping 해서 사용하기 (0) | 2024.06.11 |
[SwiftUI] 조절가능한 BottomSheetView (IOS 16 미만) (0) | 2024.06.10 |
[SwiftUI] PHPickerViewController 멀티선택 사진앨범 가져오기(IOS 14이상) (0) | 2024.05.31 |