如何在NavigationView中删除Navbar
我已经在此处询问当我从API中获取数据时,使用页面样式tabview 时如何摆脱范围索引错误。我建议这样做:
如果rockets.isempty 子句,
整个 tabview 并显示 如果数组为空(例如加载指示灯)
// MARK: - API
class InfoApi {
func getRockets(completion: @escaping ([RocketInfo]) -> ()) {
guard let url = URL(string: "https://api.spacexdata.com/v4/rockets") else {
return
}
URLSession.shared.dataTask(with: url) { (data, response, error) in
do {
let rocketsInfo = try JSONDecoder().decode([RocketInfo].self, from: data!)
DispatchQueue.main.async {
completion(rocketsInfo)
}
} catch {
print(error.localizedDescription)
}
}
.resume()
}
}
// MARK: - ROCKET MODEL
struct RocketInfo: Codable, Identifiable {
let id = UUID()
let name: String
let country: String
}
// MARK: - CONTENT VIEW
struct ContentView: View {
@State var rockets: [RocketInfo] = []
var body: some View {
Group {
if rockets.isEmpty {
ProgressView()
} else {
NavigationView {
TabView {
ForEach(rockets) { rocket in
ScrollView(.vertical, showsIndicators: false) {
VStack(alignment: .center) {
Image(systemName: "globe")
.renderingMode(.original)
.resizable()
.scaledToFit()
.frame(height: 190, alignment: .center)
ZStack {
RoundedRectangle(cornerRadius: 32)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 865, maxHeight: .infinity, alignment: .center)
.foregroundColor(Color.gray)
VStack(spacing: 40) {
HStack {
Text("Name")
.font(.title)
Spacer()
Text(rocket.name)
.font(.title)
}
} //: VSTACK
.padding(.horizontal)
} //: ZSTACK
} //: VSTACK
.navigationBarTitleDisplayMode(.inline)
.navigationBarHidden(true)
} //: SCROLL
} //: LOOP
} //: TAB
.tabViewStyle(.page)
.edgesIgnoringSafeArea(.vertical)
} //: NAVIGATION
}
} //: GROUP
.onAppear {
InfoApi().getRockets { rockets in
self.rockets = rockets
}
}
.edgesIgnoringSafeArea(.vertical)
}
}
// MARK: - PREVIEW
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
有所帮助,但是还有另一个问题。要在 if-else 子句中包装TabView,我必须在其外部添加其他视图(例如, navigationView ),并致电 onappear 这种观点使一切正常的方法。我无法完全删除 NavigationView 顶部的白色安全区域的事实存在问题。
如果我包裹 tabview hstack 而不是 navigationView ,然后在将scrollview滚动到末端时,我看到了一个白色的条,我也不知道该如何摆脱。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有许多潜在的解决方案。所有这些都涉及摆脱
navigationView
,因为似乎您唯一使用的是您需要某种包装器视图。解决方案#1:
移动
isempty
在TabView
解决方案> 2:2:
而不是
NavigationView
,将您的内容包装在group
:解决方案#3:call
onappear
出现时,模拟对象/API
There are a number of potential solutions to this. All involve getting rid of the
NavigationView
since it seems that your only use for it was that you needed some sort of wrapper view.Solution #1:
Move your
isEmpty
check inside theTabView
Solution #2:
Instead of
NavigationView
, wrap your content in aGroup
:Solution #3: call
onAppear
on the progress view when it appearsEdit: complete example with mocked objects/API
不幸的是,事实证明,如果您在一个视图中同时使用 TabBar 和 NavigationView,则完全不可能摆脱导航栏。
Unfortunately, it turned out that it is quite impossible to get rid of navbar if you use both TabBar and NavigationView in one View.