首页 > 解决方案 > 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)
    }
}

标签: swiftswiftuiswiftui-navigationviewswiftui-button

解决方案


如果你添加一个

.zIndex(1)

你的修饰符HStack,它应该可以工作。


推荐阅读