VStack 위에있는 SwiftUI 신비한 공간
Aug 20 2020
이 코드가 있습니다
NavigationView{
VStack{
GeometryReader{ geometry in
VStack{
Text("a")
}
.frame(width:geometry.size.width)
.background(Color.orange)
Spacer()
}
}
.modifier(NavBarModifier(font: self.fontUI,text: "Profile"))
}
.navigationViewStyle(StackNavigationViewStyle())
GeometryReader가 VStack 상단에 나타나기를 원하지만 결과는 다음과 같습니다.

파란색 선이 VStack의 상단이고 주황색은 GeometryReader입니다. GeometryReader 뒤에 Spacer ()를 추가하려고했지만 작동하지 않았습니다. 그 간격을 어떻게 제거 할 수 있습니까?
struct NavBarModifier: ViewModifier{
var font: UIFont
var text: String
func body(content: Content) -> some View {
return content
.zIndex(0)
.animation(.spring())
.padding(.top,80)
.navigationBarTitle(Text(self.text),displayMode: .inline)
.navigationBarHidden(false)
.foregroundColor(.orange)
.background(NavigationConfigurator { nc in
nc.navigationBar.barTintColor = UIColor(red: 243/255, green: 107/255, blue: 21/255, alpha: 1)
nc.navigationBar.titleTextAttributes = [
.foregroundColor : UIColor.white,
.font : self.font,
.kern: 1.2
]
}
.padding([.top, .leading, .trailing]))
}
}
답변
1 Asperi Aug 20 2020 at 16:32
중복 패딩을 제거하십시오. NavBarModifier
func body(content: Content) -> some View {
return content
.zIndex(0)
.animation(.spring())
// .padding(.top,80) // << this one !!
ignalonzo Aug 20 2020 at 15:54
당신은의 상단에 배치에의 GeometryReader 후 스페이서를 추가 할 필요 VStack
도 추가 .edgesIgnoringSafeArea(.top)
가 그 공백 (안전 영역)을 무시하고는 상단에 정렬 할 수 있도록 VStack
.
VStack {
GeometryReader{ geometry in
VStack {
Text("s")
}
.frame(width:geometry.size.width)
.background(Color.orange)
Spacer()
}
.edgesIgnoringSafeArea(.top)
Spacer()
}