简体   繁体   English

如何在 SwiftUI 表单上放置背景图像

[英]How to put background image on SwiftUI Form

I have the below following code and I want to put some image to the area that highlighted in red.我有以下代码,我想将一些图像放在以红色突出显示的区域。 How can I do that in SwiftUI?我怎么能在 SwiftUI 中做到这一点?

Here is highlighted red area example: https://ibb.co/6X2zvyq这是突出显示的红色区域示例: https : //ibb.co/6X2zvyq

struct ContentView: View {
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Info")) {
                    HStack {
                        Text("Name")
                        Spacer()
                        Text("someName")
                    }
                    HStack {
                        Text("Surname")
                        Spacer()
                        Text("someSurname")
                    }
                }
            }
            .navigationBarTitle("Profile")
        }
    }
}

Here is complete one module code.这是完整的一个模块代码。 Tested with Xcode 11.7.使用 Xcode 11.7 测试。 Image named "plant" is located inside Assets.xcassets名为“植物”的图像位于 Assets.xcassets 中

演示

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()

        let appearance = UINavigationBarAppearance()
        appearance.configureWithTransparentBackground()
        appearance.backgroundImage = UIImage(named: "plant")

        navigationBar.standardAppearance = appearance
        navigationBar.compactAppearance = appearance
        navigationBar.scrollEdgeAppearance = appearance
    }
}

struct ContentView: View {
    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Info")) {
                    HStack {
                        Text("Name")
                        Spacer()
                        Text("someName")
                    }
                    HStack {
                        Text("Surname")
                        Spacer()
                        Text("someSurname")
                    }
                }
            }
            .navigationBarTitle("Profile")
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM