Anj*_*iya 3 user-interface textfield ios swift swiftui
我正在SignUp使用 SwiftUI 框架创建一个屏幕。
我有两个文本框,FirstName并LastName和一个按钮。
我将它们分组在一个VStack.
当我运行应用程序时,该按钮是可点击的,但我无法在文本字段中输入任何内容。
这是我的代码:
@State var firstName = ""
@State var lastName = ""
var body: some View {
NavigationView {
VStack {
VStack {
VStack {
Group {
TextField($firstName, placeholder: Text("First Name")).padding(10)
}.background(Color.white).clipShape(RoundedRectangle(cornerRadius: 5))
.shadow(radius: 3)
Group {
TextField($lastName, placeholder: Text("Last Name")).padding(10)
}.background(Color.white).clipShape(RoundedRectangle(cornerRadius: 5))
.shadow(radius: 3)
Button(action: {
}) {
Group {
Text("Create User").color(.white).padding(10)
}.background(Color.blue).clipShape(RoundedRectangle(cornerRadius: 5))
.shadow(radius: 5)
}
}.padding(12)
.background(Color.gray)
.shadow(radius: 5)
}.background(Color.gray)
}.navigationBarTitle(Text("Sign Up"))
}
}
Run Code Online (Sandbox Code Playgroud)
shadow在这种特定情况下,似乎在颜色/不透明度效果之后设置会导致问题。
为了确保合成效果在阴影之前呈现,您可以添加.compositingGroup()到您的VStack- 就在shadow- 并解决问题。
文档说:
合成组使此视图的祖先视图中的合成效果(如不透明度和混合模式)在此视图渲染之前生效。
修改后的代码:
VStack {
// [...]
}
.padding(12)
.background(Color.gray)
.compositingGroup() // THIS
.shadow(radius: 5)
.navigationBarTitle(Text("Sign Up"))
Run Code Online (Sandbox Code Playgroud)
用户界面可以通过这种方式进一步简化 - 它实现了相同的结果。
struct ContentView: View {
@State var firstName = ""
@State var lastName = ""
var body: some View {
NavigationView {
VStack {
TextField($firstName, placeholder: Text("First Name"))
.padding(10)
.background(Color.white)
.cornerRadius(5)
.shadow(radius: 3)
TextField($lastName, placeholder: Text("Last Name"))
.padding(10)
.background(Color.white)
.cornerRadius(5)
.shadow(radius: 3)
Button(action: { }) {
Text("Create User")
.color(.white)
.padding(10)
.background(Color.blue)
.cornerRadius(5)
.shadow(radius: 5)
}
}
.padding(12)
.background(Color.gray)
.compositingGroup()
.shadow(radius: 5)
.navigationBarTitle(Text("Sign Up"))
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
948 次 |
| 最近记录: |