xcode SwiftUI-如何使背景颜色填满整个屏幕

9wbgstp7  于 2022-11-26  发布在  Swift
关注(0)|答案(2)|浏览(242)

我正在尝试制作一个应用程序,我想有一个应用程序的背景颜色,所以我把颜色在资产文件夹,但颜色只填充了屏幕的四分之三,离开顶部一半空。
我有一个ZStack,所以我把.background(Color("appBackground"))放在ZStack的末尾,这给我留下了这个结果。
整个HomeScreenView的程式码:

import AVKit
import SwiftUI

struct HomeView: View {
    
    @Binding var streak: Streaks
    @Binding var timer: TimerStruct
    @State var isSheetPresented = false
    @Binding var navigationPath: NavigationPath
    @Binding var exercisePlans: [ExercisePlan]
    
    func ridzero(result: Double) -> String {
        let value = String(format: "%g", result)
        return value
    }
    func round(result: Double) -> String {
        let value = String(format: "%.1f", result)
        return value
    }
    
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                ScrollView {
                    ZStack {
                        VStack {
                            
                            let percent = Double(timer.exerciseTime/1500)
                            Text("Welcome back to ElderlyFit")
                                .font(.system(size: 25,weight: .medium, design: .rounded))
                                .offset(x: 0, y: 20)

                            CircularProgressView(timer: $timer, progress: CGFloat(percent))
                                .frame(width: 150, height: 150)
                                .offset(x: -95, y: -240)
                                .padding(EdgeInsets(top: 280, leading: 0, bottom: 0, trailing: 0))
                            Text("\(round(result:percent*100))%")
                                .font(.system(size: 30, weight: .bold, design: .rounded))
                                .offset(x:-92, y:-345)
                            
                            Text("\(round(result: timer.exerciseTime/60)) mins of exercise completed today")
                                .frame(width: 200, height: 50)
                                .font(.system(size: 20, design: .rounded))
                                .offset(x:100, y:-410)
                            
                            
                            StreaksView(timer: $timer, streak: $streak)
                                .offset(x:0, y: -330)
                                .padding()
                            
                            Text("Choose your exercise plan:")
                                .bold()
                                .font(.system(size: 25))
                                .offset(x: -30, y: -420)
                                .zIndex(1.0)
                            
                            
                            ExercisePlanView( streaks: $streak, timer: $timer, navigationPath: $navigationPath, exercisePlans: $exercisePlans)
                                .offset(x: 15, y: -405)
                                .zIndex(-1.0)
                                .font(Font.system(size: UIFontMetrics.default.scaledValue(for: 15)))
                            
                            //.frame(maxWidth: .infinity, maxHeight: .infinity)
                            
                        }
                        .frame(width: geometry.size.width)
                        .edgesIgnoringSafeArea(.all)
                        
                    }
                    .background(Color("appBackground"))
                }
            }
        }
    }
}

mzmfm0qo

mzmfm0qo1#

删除.background(Color(“appBackground”))行并将其直接放在ZStack下:

Color("appBackground").edgesIgnoringSafeArea(.all)
628mspwn

628mspwn2#

您还需要在background之后使用edgesIgnoringSafeArea(.all)
应该是

.background(Color("appBackground")).edgesIgnoringSafeArea(.all)

此外,由于您使用的是NavigationPath,它只适用于iOS 16+。可能使用ignoresSafeArea()会更好,因为edgesIgnoringSafeArea(.all)自iOS 14以来已被弃用。
而且会是

.background(Color("appBackground")).ignoresSafeArea()

相关问题