swift - iOS | 斯威夫特 | 按钮在 HStack 中不起作用 | 我该如何解决这个问题,有解决方法吗?
问题描述
我已经尝试并搜索了相当多的stackOverflow和其他地方,虽然有一些类似的问题,他们试图将按钮的样式更改为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
,它应该可以工作。
推荐阅读
- javascript - 更新前端的 ACF 字段设置
- python - 将嵌套的 json 转换为 csv 数据帧
- http - 在 GET 请求中伪造国家
- firewall - 如何检查哪些是旧网络以启用防火墙日志记录?
- python - 实施 sched 时参数无效
- macos - 错误:没有可用的公式或名称为“apache-spark@2.4.3”的木桶
- java - 对于 Runnable 类型,方法 start() 未定义
- python - Pytest 和子模块
- powerbi - 如何计算 DAX 中获得批准或状态更改的项目数量
- java - 具有手动提交偏移量的kafka多线程消费者:KafkaConsumer对于多线程访问是不安全的