iOS |迅捷|按钮在 HStack 中不起作用 |我该如何解决这个问题,有解决方法吗?
iOS | SWIFTUI | Buttons don't work in the HStack | How can I fix this, Is there a workaround.?
我已经尝试并搜索了相当多的Whosebug和其他地方,同时也有一些类似的问题,他们试图将按钮的样式更改为Plain of Default,我尝试了这些方法,他们似乎没有工作。
对于上下文,这是 .sheet 视图中的一个视图,如果这可能是此处的问题。
我需要这方面的帮助。谢谢。
编辑:我尝试在三个按钮之间创建间距,“高”和“低”按钮有效,但“中”按钮无效。
答案:将 zIndex(1) 添加到 HStack。
import SwiftUI
struct AddTasks: View {
@State var data = 0
@State var taskName = ""
@State private var prioritySelectedHigh : Bool = false
@State private var prioritySelectedMed : Bool = false
@State private var prioritySelectedLow : Bool = false
@State private var priority : String = ""
var items = [1,2,3,4,5,6,7,8,9,10]
var body: some View {
NavigationView {
VStack(alignment:.center, spacing: 30){
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(Color("TabBar "))
.opacity(0.7)
.frame(width: 310, height: 60, alignment: .center)
TextField(" Task Name ", text: $taskName)
.frame(width: 270, height: 60, alignment: .center)
}
// These Buttons dont work
HStack(alignment: .center, spacing: 52){
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.pink)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action: {
print("Hi")
}, label: {
Text("High")
.font(.system(size: 18))
.foregroundColor(.red)
})
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.orange)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action: {}, label: {
Text("Med")
.font(.system(size: 18))
.foregroundColor(.orange)
})
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.green)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action:{}, label: {
Text("Low")
.font(.system(size: 18))
.foregroundColor(.green)
})
}
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(Color("TabBar "))
.opacity(0.7)
.frame(width: 310, height: 60, alignment: .center)
Picker(selection: $data, label: Text("Data"), content: {
ForEach(items, id:\.self) { item in
Text("\(item)")
.rotationEffect(Angle(degrees: 90))
}
})
.labelsHidden()
.rotationEffect(Angle(degrees: -90))
.frame(maxHeight: 60)
.clipped()
.foregroundColor(Color("AccentColor"))
.font(.system(size: 20))
}
}.navigationTitle("New Task")
}.environment(\.colorScheme, .dark)
}
}
struct AddTasks_Previews: PreviewProvider {
static var previews: some View {
AddTasks()
// .environment(\.colorScheme, .dark)
}
}
如果你加一个
.zIndex(1)
修改你的 HStack
,它应该可以工作。
我已经尝试并搜索了相当多的Whosebug和其他地方,同时也有一些类似的问题,他们试图将按钮的样式更改为Plain of Default,我尝试了这些方法,他们似乎没有工作。
对于上下文,这是 .sheet 视图中的一个视图,如果这可能是此处的问题。
我需要这方面的帮助。谢谢。
编辑:我尝试在三个按钮之间创建间距,“高”和“低”按钮有效,但“中”按钮无效。
答案:将 zIndex(1) 添加到 HStack。
import SwiftUI
struct AddTasks: View {
@State var data = 0
@State var taskName = ""
@State private var prioritySelectedHigh : Bool = false
@State private var prioritySelectedMed : Bool = false
@State private var prioritySelectedLow : Bool = false
@State private var priority : String = ""
var items = [1,2,3,4,5,6,7,8,9,10]
var body: some View {
NavigationView {
VStack(alignment:.center, spacing: 30){
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(Color("TabBar "))
.opacity(0.7)
.frame(width: 310, height: 60, alignment: .center)
TextField(" Task Name ", text: $taskName)
.frame(width: 270, height: 60, alignment: .center)
}
// These Buttons dont work
HStack(alignment: .center, spacing: 52){
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.pink)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action: {
print("Hi")
}, label: {
Text("High")
.font(.system(size: 18))
.foregroundColor(.red)
})
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.orange)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action: {}, label: {
Text("Med")
.font(.system(size: 18))
.foregroundColor(.orange)
})
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(.green)
.opacity(0.2)
.frame(width: 70, height: 40, alignment: .center)
Button(action:{}, label: {
Text("Low")
.font(.system(size: 18))
.foregroundColor(.green)
})
}
}
ZStack {
RoundedRectangle(cornerRadius: 9)
.foregroundColor(Color("TabBar "))
.opacity(0.7)
.frame(width: 310, height: 60, alignment: .center)
Picker(selection: $data, label: Text("Data"), content: {
ForEach(items, id:\.self) { item in
Text("\(item)")
.rotationEffect(Angle(degrees: 90))
}
})
.labelsHidden()
.rotationEffect(Angle(degrees: -90))
.frame(maxHeight: 60)
.clipped()
.foregroundColor(Color("AccentColor"))
.font(.system(size: 20))
}
}.navigationTitle("New Task")
}.environment(\.colorScheme, .dark)
}
}
struct AddTasks_Previews: PreviewProvider {
static var previews: some View {
AddTasks()
// .environment(\.colorScheme, .dark)
}
}
如果你加一个
.zIndex(1)
修改你的 HStack
,它应该可以工作。