swift 当文本字段处于活动状态时,内容显示在导航栏后面

siotufzp  于 11个月前  发布在  Swift
关注(0)|答案(1)|浏览(108)

当我在我的视图中使用文本字段时,在自定义导航栏后面可以看到vstack或scrollview中的内容。如何解决此背景问题?

当前视图中的代码

import SwiftUI
import MapKit

struct address_edit_address: View {

@State private var street: String = ""
@State private var titel: String = ""
@State private var comment: String = ""

@State var topLeft: CGFloat = 0
@State var topRight: CGFloat = 50
@State var bottomLeft: CGFloat = 50
@State var bottomRight: CGFloat = 0

@State private var region = MKCoordinateRegion(center:     CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5))

var body: some View {
    VStack{
        VStack(alignment: .leading, spacing: 20){
            Map(coordinateRegion: $region).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 128, maxHeight: 128).cornerRadius(10)
            VStack (alignment: .leading, spacing: 10){
            Text("Adresse:").font(.custom("Arboria-Medium", size:13))
            HStack (alignment: .center){
            TextField("Straße und Hausnummer*", text: $street).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 54, maxHeight: 54, alignment: .center)
                .foregroundColor(Color("white"))
                .background(RoundedRectangle(cornerRadius:10).fill(Color("grey_dark1_bg_obj")))
                .overlay(
                    RoundedRectangle(cornerRadius:10).stroke(Color("grey_light2_obj_font"), lineWidth: 1))

            }
            }
            VStack (alignment: .leading, spacing: 10){
            Text("Adresstitel & Typ(optional):").font(.custom("Arboria-Medium", size:13))
            HStack(spacing: 0){
                TextField("Titel", text: $titel).frame(minWidth: 0, idealWidth: 230, maxWidth: 230, minHeight: 0, idealHeight: 54, maxHeight: 54, alignment: .leading)
                    .foregroundColor(Color("white"))
                    .background(RoundedCornerShape(radius:10, corners: [.topLeft, .bottomLeft]).fill(Color("grey_dark1_bg_obj")))
                    .overlay(
                        RoundedCornerShape(radius: 10, corners: [.topLeft, .bottomLeft]).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
                HStack(spacing: 0){
                    Button(action: {
                        print("Privat")
                    }){ VStack {
                        Image(systemName: "house.fill")
                        Text("Privat").font(.custom("Arboria-Medium", size:12))
                    }
                    }.frame(width: 74, height: 56).background(Color("blue")).border(Color("grey_light2_obj_font"), width: 1)
                    Button(action: {
                        print("Arbeit")
                    }){ VStack{
                        Image(systemName: "briefcase.fill")
                        Text("Arbeit").font(.custom("Arboria-Medium", size:12))
                    }
                        
                    }.frame(width: 74, height: 54)
                    .background(RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight]).fill(Color("grey_dark1_bg_obj")))
                    .overlay(
                        RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight]).stroke(Color("grey_light2_obj_font"), lineWidth: 1))
                }.frame(width: 148, height: 51, alignment: .leading)
                
            }
            }
            VStack (alignment: .leading, spacing: 10){
            Text("Anmerkung hinzufügen(optional):").font(.custom("Arboria-Medium", size:13))
            HStack{

            TextField("Anmerkung zur Lieferung", text: $comment).frame(minWidth: 0, idealWidth: 383, maxWidth: 383, minHeight: 0, idealHeight: 102, maxHeight: 102, alignment: .center)
                .foregroundColor(Color("white"))
                .background(RoundedRectangle(cornerRadius:10).fill(Color("grey_dark1_bg_obj")))
                .overlay(
                    RoundedRectangle(cornerRadius:10).stroke(Color("grey_light2_obj_font"), lineWidth: 1))

            }
            HStack{
                Text("*Pflichtfelder")
                Spacer()
                Text("0/180")
            }.font(.custom("Arboria-Medium", size:13))
            
            
            
            
            }.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 600, alignment: .top)
        }
        VStack(spacing: 30){
        HStack(spacing: 20){
        Button("Speichern"){
            
        }.frame(minWidth: 0, idealWidth: 160, maxWidth: 160, minHeight: 0, idealHeight: 43, maxHeight: 43, alignment: .center).background(Color("green_mid")).clipShape(RoundedRectangle(cornerRadius: 10)).font(.custom("Arboria-Medium", size: 14))
            Button (action: {
                print("Favorit")
            }){ HStack {
                Text("Favorisieren").font(.custom("Arboria-Medium", size: 14))
                Image(systemName:"heart")
            }.foregroundColor(Color("yellow"))
                
            }.frame(minWidth: 0, idealWidth: 160, maxWidth: 160, minHeight: 0, idealHeight: 43, maxHeight: 43, alignment: .center).overlay(
                RoundedCornerShape(radius: 10, corners: [.topRight, .bottomRight, .topLeft, .bottomLeft]).stroke(Color("yellow"), lineWidth: 1))

        }
        Button("Adresse entfernen"){
            
        }.foregroundColor(Color("red")).font(.custom("Arboria-Medium", size: 14))
        }
        Spacer()
    }.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight).background(Color.black) .foregroundColor(Color.white)
}
}

 struct address_edit_address_Previews: PreviewProvider {
 static var previews: some View {
    address_edit_address().preferredColorScheme(.dark)
}
}

来自模板的代码,导航栏在其中实现

import SwiftUI

struct account_template<Content: View>: View {
@Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>

@ViewBuilder var content: Content
var title: String
var scrollable: Bool

init(content:Content, title:String, scrollable: Bool){
    self.content = content
    self.title = title
    self.scrollable = scrollable
}

var btnBack : some View {
    Button(action: {
        self.presentationMode.wrappedValue.dismiss()
    }){
        VStack{
        ZStack{
            HStack(){
                Image(systemName: "arrow.left")
                    .aspectRatio(contentMode: .fit)
                    .foregroundColor(.white)
                Spacer()
            }.frame(width: UIScreen.screenWidth)
           
            
            HStack (alignment:.center){
                Text(title).foregroundColor(.white).font(.custom("Arboria-Medium", size:16))
         
            }.frame(width: UIScreen.screenWidth)
        }.frame(width: UIScreen.screenWidth, height: 50).background(Color("black"))
            Divider().background(Color("white")).frame(height:2)
        }.frame(height: 75, alignment: .center).background(Color.black).edgesIgnoringSafeArea(.top)
    }
}

var body: some View {
    VStack {
        Spacer()
        Divider().background(Color("white")).frame(height:2)
        if scrollable {
            ScrollView(showsIndicators:false){
                content
            }.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 75, alignment: .top)
        }else{
            VStack{
                content
            }.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight - 75, alignment: .top)
        }
        
    }.background(Color.black) .foregroundColor(Color.white) .frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight, alignment: .center).navigationBarBackButtonHidden(true)
        .navigationBarItems(trailing: btnBack)

    
}

}

struct adress_template_Previews: PreviewProvider {
static var previews: some View {
    account_template(content: address_add_address(), title: "Adresse hinzufügen", scrollable: true)
    
}
}
lhcgjxsq

lhcgjxsq1#

  • 一种方法是将VStack{}内容向下移动足够的距离,以便导航栏始终可见。即,将VStack{}向下移动10%。
  • 为此,将Spacer()作为VStack{}中的第一个元素。
Spacer().frame(height: UIScreen.main.bounds.size.height * 0.1)

相关问题