Swift UI工具栏菜单中的切换按钮

huangapple go评论81阅读模式
英文:

Swift UI Toggle inside of a toolbar Menu

问题

无法在工具栏中放置的菜单中使切换开关工作。我注意到如果不设置.toggleStyle(.switch),它将允许您使用复选标记切换开关。然而,我想要传统的切换开关样式。我尝试了创建自定义按钮、标签和图像,但都没有成功。任何帮助将不胜感激。(编辑 - 需要的解决方案应与iOS 15.0兼容)

启用开关时的效果

没有.toggleStyle(.switch)的效果

我尝试了标签、按钮、图像以及构建自定义切换开关,但在菜单中似乎存在问题。我期望在菜单中看到类似下面的滑块:

普通切换开关

英文:

I can't seem to get a switch toggle to work inside of a Menu placed in the toolbar. I did notice that if I do not set the .toggleStyle(.switch) it will allow you to toggle on and off with a check mark. I would like the traditional switch style however. I have tried creating custom buttons, labels images to no avail. Any help would be appreciated. (Edit - Will need the solution to be compatible with iOS 15.0)

struct TestingView: View {
    @State var isToggled: Bool = false
    
    var body: some View {
        NavigationView{
            Text("List Goes Here")
                .toolbar{
                    ToolbarItem(placement: .navigationBarTrailing){
                        Menu{
                            Toggle("Test", isOn:$isToggled)
                                .toggleStyle(.switch)
                        } label: {
                            Image(systemName: "ellipsis.circle")
                                .font(.title3)
                        }
                    }
                }
        }

    }
}

struct TestingView_Previews: PreviewProvider {
    static var previews: some View {
        TestingView()
    }
}`

with switch enabled

with out .toggleStyle(.switch)

I have tried Labels, Buttons, Images, Building a Custom toggle but there seems to be a problem inside of the menu. I would expect a slider like the below within the menu:

Normal Toggle

答案1

得分: 1

你可以使用.popover替代Menu吗?
像这样:

import SwiftUI

struct TestingView: View {
    @State var isToggled: Bool = false
    @State private var isPresented: Bool = false
    
    var body: some View {
        NavigationView{
            Text("List Goes Here")
                .toolbar{
                    ToolbarItem(placement: .navigationBarTrailing){
                        Image(systemName: "ellipsis.circle")
                            .font(.title3)
                            .onTapGesture {
                                isPresented.toggle()
                            }
                            .popover(isPresented: $isPresented) {
                                Toggle("Test", isOn:$isToggled)
                                    .presentationCompactAdaptation((.popover))
                            }
                    }
                }
        }
    }
}

struct TestingView_Previews: PreviewProvider {
    static var previews: some View {
        TestingView()
    }
}
英文:

Can you use a .popover instead of Menu?
Like this:

import SwiftUI

struct TestingView: View {
    @State var isToggled: Bool = false
    @State private var isPresented: Bool = false
    
    var body: some View {
        NavigationView{
            Text("List Goes Here")
                .toolbar{
                    ToolbarItem(placement: .navigationBarTrailing){
                        Image(systemName: "ellipsis.circle")
                        .font(.title3)
                        .onTapGesture {
                            isPresented.toggle()
                        }
                        .popover(isPresented: $isPresented) {
                            Toggle("Test", isOn:$isToggled)
                                .presentationCompactAdaptation((.popover))
                        }
                        
                    }
                }
        }

    }
}

struct TestingView_Previews: PreviewProvider {
    static var previews: some View {
        TestingView()
    }
}

Swift UI工具栏菜单中的切换按钮

huangapple
  • 本文由 发表于 2023年7月7日 05:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76632579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定