簡體   English   中英

SwiftUI 刪除視圖上的空格

[英]SwiftUI removing a space on the view

在 SwiftUI 視圖中,我嘗試顯示一個簡單的 TOP 藍色條、一個紅色矩形和一個下條,正如您從所附圖片中看到的那樣,頂部有一個奇怪的空白區域,頂部的紅色矩形是我的想刪除,但我不明白為什么會在那里。

快速界面

這是簡單的代碼:

    var body: some View {

        GeometryReader { geometry in
            VStack {
                //upper top bar
                HStack{
                    Text("TOP BAR")

                }.frame(width: geometry.size.width, height: geometry.size.height/10)
                    .background(LinearGradient(gradient: Gradient(colors: [Color("BlueW"), Color("DARK")]), startPoint: .topLeading, endPoint: .bottomTrailing).shadow(radius: 2)).edgesIgnoringSafeArea(.all)

// <<<<<<<<<<<<<<<<<<< PROBLEM HERE <<<<<<<<<<<<<<<<<<
                // testing rectangle
                Rectangle().foregroundColor(.red)

                //lower bar
                ZStack {

                    HStack {
                        Image(systemName: "house")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .padding(20)
                            .frame(width: geometry.size.width/3, height: 75)
                            .foregroundColor(self.viewRouter.currentView == "home" ? .black : .gray)
                            .onTapGesture {
                                self.viewRouter.currentView = "home"
                        }
                        ZStack {
                            Circle()
                                .foregroundColor(Color.white)
                                .frame(width: 75, height: 75)
                            Button(action: {
                                self.isAddPresented = true
                            }) {
                                Image(systemName: "plus.circle.fill")
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(width: 75, height: 75)
                                    .foregroundColor(.green)
                                    .rotationEffect(Angle(degrees: self.showPopUp ? 90 : 0))
                            }
                            .sheet(isPresented: self.$isAddPresented) {
                                AirportView(dm: self.dm, airport: self.general.airportData, dismissFlag: self.$isAddPresented)

                            }
                        }

                        .offset(y: -geometry.size.height/10/2)
                        .onTapGesture {
                            withAnimation {
                                self.showPopUp.toggle()
                            }

                        }
                        Image(systemName: "paperplane.fill")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .padding(20)
                            .frame(width: geometry.size.width/3, height: 75)
                            .foregroundColor(self.viewRouter.currentView == "routekit" ? .black : .gray)
                            .onTapGesture {
                                self.viewRouter.currentView = "routekit"
                        }

                    }.frame(width: geometry.size.width, height: geometry.size.height/10)
                        .background(LinearGradient(gradient: Gradient(colors: [Color("BlueW"), Color("DARK")]), startPoint: .topLeading, endPoint: .bottomTrailing).shadow(radius: 2))
                }
            }

        }.edgesIgnoringSafeArea(.bottom)
    }

問題在於HStack的邊緣忽略了所有安全區域( .edgesIgnoringSafeArea(.all) )。

在此處輸入圖片說明

如您所見,如果您檢查您的視圖,您的綠色視圖 ( HStack ) 實際上在GeometryReader內部,但它的框架在外面。

解決方案:刪除.edgesIgnoringSafeArea(.all)

但是,新問題是頂部安全區域會有一個空白區域。

解決方案:

GeometryReader設置為.edgesIgnoringSafeArea([.top, .bottom])

瞧!

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM