为什么 swiftui 中的导航视图栏这么大?

Use*_*149 1 navigation navigationbar swift swiftui

import SwiftUI
struct SecondView: View {

var ResearchMCQ: Question


//Creating Variables for Revision Topics
@State private var setOptionOne = false
@State private var setOptionTwo = false
@State private var setOptionThree = false
    
//User Home Page View
        var body: some View {
            
//Allows for Navigation and Scrolling
        NavigationView {
        ScrollView{
            
//App Logo and Vertical Stacks
            VStack(spacing: 1.0) {
            Image("AppLogo")
                    .resizable()
                    .scaledToFit()
                    .padding(.trailing, 50.0)
                .frame(height: 100, alignment: .topLeading)

    Spacer()
    Spacer()
                
//Multiple Choice Question Appears
    Group {
    Text(ResearchMCQ.question)
    .padding(.trailing, 4)
    
    Spacer()
    Spacer()
        
//Ensures Only One Answer Can Be Selected
        let OptionOne = Binding<Bool>(get: { self.setOptionOne }, set: { self.setOptionOne = $0; self.setOptionTwo = false; self.setOptionThree = false })
        let OptionTwo = Binding<Bool>(get: { self.setOptionTwo }, set: { self.setOptionOne = false; self.setOptionTwo = $0; self.setOptionThree = false })
        let OptionThree = Binding<Bool>(get: { self.setOptionThree }, set: { self.setOptionOne = false; self.setOptionTwo = false; self.setOptionThree = $0 })

//Shows User MCQ Options
        VStack {
            Toggle(ResearchMCQ.options[0], isOn: OptionOne)
                .toggleStyle(.button)
                .tint(Color(.gray))
                .foregroundColor(Color("Black-White"))
            Toggle(ResearchMCQ.options[1], isOn: OptionTwo)
                .toggleStyle(.button)
                .tint(Color(.gray))
                .foregroundColor(Color("Black-White"))
            Toggle(ResearchMCQ.options[2], isOn: OptionThree)
                .toggleStyle(.button)
                .tint(Color(.gray))
                .foregroundColor(Color("Black-White"))
                 }
    }
                
                
        }
            
           // .padding(.top, -150)
    }
}
            
//Allows Navigation Through Pages
   .navigationTitle("")
    .padding(.top, -100)
            
        }
Run Code Online (Sandbox Code Playgroud)

}

显示大导航栏

我正在尝试创建一个导航栏,但是我希望它为空,因此代码中的引号为空。关于如何使其变小的任何想法,当我如图所示向上滚动时,导航栏占据了页面的很大一部分。作为第一次快速编码,任何帮助将不胜感激!

ton*_*ibi 6

您只需要一个导航视图。我猜你的 FirstView 中也有一个 NavigationView 。这已经足够了,因为这个顶部的 NavigationView 将包含您所有更深层次的视图。

所以你不需要NavigationView {}在 SecondView 中。仅在您的 FirstView 中需要它。只需将其从 SecondView 中删除,您将拥有一个较小的导航栏。

.navigationTitle("")应该位于您的 NavigationView内部,而不是作为其自身的修饰符:

struct FirstView: View {
    var body: some View {
        NavigationView {
            NavigationLink("second View", destination: SecondView())
                .navigationTitle("")
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我修改了您的代码,因为我没有 Question 类/结构,但它应该与您的代码相同

struct SecondView: View {
    var body: some View {
        // I removed the NavigationView that was there
        ScrollView{
            VStack {
                Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.")
                Toggle("Toogle1", isOn: .constant(true))
                Toggle("Toogle2", isOn: .constant(false))
                Toggle("Toogle3", isOn: .constant(true))
            }
        }.navigationTitle("")
    }
}
Run Code Online (Sandbox Code Playgroud)

结果 :

结果