SwiftUi - 无法更改/无法访问背景

标签 swift xcode uinavigationcontroller swiftui xcode11

我的背景有问题。 有几天我在玩 SwiftUi,一切都很好。

昨天我用背景填满了我的屏幕,但只有顶部配件不起作用,所以我撤消了我的更改。现在的问题是:我无法再访问我的背景...

其实我无法达到我的背景,所以我无法改变它。

这是我的代码:

struct ContentView : View {
@State private var name = String()
@State private var pw = String()

var body: some View {
    VStack{
        NavigationView {

            Spacer()
            //Einleitung
            VStack{
                Image("user")
                    .resizable()
                    .frame(width: 100 , height: 100)
                Spacer()
                Text("Hello User.")
                Text("Please insert your Nickname and your Password")
                    .lineLimit(nil)
            }.background(Color.blue)
            .padding()
            //TextField eingabe für Name + Passwort
            HStack{
                Spacer()
                Text("Username")
                .font(.headline)
                Spacer()
            Text("Password")
                .font(.headline)
                Spacer()
            }.background(Color.red)
            HStack{
                Spacer()
                TextField($name,placeholder: Text("Fill your name"))
                    .textFieldStyle(.roundedBorder)
                    .scaledToFit()
                Spacer()
                SecureField($pw, placeholder: Text("123456789"))
                    .scaledToFit()
                    .textFieldStyle(.roundedBorder)
                    Spacer()
                }.background(Color.green)
            //LOGIN BTN
            Spacer()
            Spacer()

            Button(action: {
                print("Hallo \(self.name) - dein Password ist \(self.pw)")
            //NavigationDestinationLink :
                })
                {
                    HStack{
                        Spacer()
                        Text("Login")
                            .color(.white)
                            .bold()
                            .font(.headline)
                            .padding(.horizontal)
                        Spacer()
                    }
                    }
                    .background(Color.red,cornerRadius: 40)
                    .frame(width: 200, alignment: .bottom)
                    Spacer()
            }.background(Color.black)
        }.background(Color.yellow)
   }
}

Actual view

最佳答案

内部 View 的背景颜色存在问题navigationView <强> I described earlier here in this answer 。它的总结是,在您要更改其背景颜色的 View 之上还有另一个白色 View 。所以你看不到颜色。

但是现在,您应该像这样重新排列您的 View ,

1- NavigationView应该是第一个

2- 其他所有内容(包括外部 VStack )都应包裹在 ZStack

3- A Color作为 ZStack 的第一个 View

var body: some View {
    NavigationView {
        ZStack {
            Color.yellow
            VStack{
                Text("Everything else")
            }
        }
    }
}

我希望 Apple 尽快为此类设置提供直接 API。

关于SwiftUi - 无法更改/无法访问背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57692888/

相关文章:

ios - 如何使用 Assets 目录作为导航栏背景图片?

swift - 如何检测推送的 View Controller 是否再次出现?

swift - 如何让UIViewController从下往上滑动而不褪色之前的 View ?

ios - 如何让 NSTimer 在满足条件后停止重复

ios - 为什么这些数字没有添加到 Xcode 调试器中?

ios - 在派生数据和应用程序(iOS、Google Drive SDK)中实现的类

ios - “pod install”卡在 'Receiving Objects'

swift - 如何在 Swift 中使用静态库和模块映射文件?

ios - 将值从自定义 View 传递到主视图 Controller - Swift

ios - 内部查询解析