英文:
Swift UI Toggle inside of a toolbar Menu
问题
无法在工具栏中放置的菜单中使切换开关工作。我注意到如果不设置.toggleStyle(.switch)
,它将允许您使用复选标记切换开关。然而,我想要传统的切换开关样式。我尝试了创建自定义按钮、标签和图像,但都没有成功。任何帮助将不胜感激。(编辑 - 需要的解决方案应与iOS 15.0兼容)
我尝试了标签、按钮、图像以及构建自定义切换开关,但在菜单中似乎存在问题。我期望在菜单中看到类似下面的滑块:
英文:
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 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:
答案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()
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论