更改 ScrollView SwiftUI 的背景颜色

时间:2021-05-02 15:50:27

标签: ios swift swiftui scrollview background-color

我是开发 IOS 应用程序的新手,我在登录页面底部遇到额外空白的问题,具体取决于屏幕上的图像大小。有人告诉我,我应该更改 ScrollView 的背景颜色并将视图的背景设置为清晰。我不确定如何更改 ScrollView 的背景颜色。我的代码基本上是这样的,为了可读性删除了一些东西:

var body: some View {
    ScrollView {
        //if login.isLoggedIn {
        MainView()
        //} else {
        VStack() {
            
        }
        
        .background(LinearGradient(gradient: Gradient(colors: [Color{.lightblue), Color(.blue)]), startPoint: .top, endPoint: .bottom))
            .edgesIgnoringSafeArea(.all)
        }
        }
}
//}
Screen with Whitespace Screen without Whitespace After Image on Screen is Made Larger

有人可以告诉我如何更改背景颜色吗?我更喜欢背景颜色是上面的线性渐变。如果没有,你能在 UIScrollView 中展示如何做到这一点吗?

1 个答案:

答案 0 :(得分:3)

尝试将背景颜色设置向下移动一行。 this post 也可能有帮助。

struct ContentView: View {
    var body: some View {
        ScrollView {
            //if login.isLoggedIn {
            MainView()
            //} else {
            VStack() {
                
            }
        }.background(
            LinearGradient(gradient: Gradient(colors: [Color(.lightblue), Color(.blue)]), startPoint: .top, endPoint: .bottom)
        ).edgesIgnoringSafeArea(.all)
    }
}

...

// Extension for custom colour lightBlue
extension UIColor {
    static let lightBlue = UIColor(red: 0.2, green: 0.6, blue: 0.8, alpha: 1)
}