admin管理员组文章数量:1422017
I'm having issues getting SwiftUI's Menu
working in a toolbar. When outside a toolbar it works fine, but when in a toolbar pressing the menu's button doesn't open the sub menu. I've reported to apple as FB16853917.
Anyone find a workaround?
Minimal reproduction:
import SwiftUI
struct MyMenu: View {
var body: some View {
Menu("Test Menu") {
Button {
print("action 1")
} label: {
Text("Sub 1")
}
Button {
print("action 2")
} label: {
Text("Sub 2")
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
MyMenu()
}
.padding()
.toolbar {
MyMenu()
}
}
}
}
I'm having issues getting SwiftUI's Menu
working in a toolbar. When outside a toolbar it works fine, but when in a toolbar pressing the menu's button doesn't open the sub menu. I've reported to apple as FB16853917.
Anyone find a workaround?
Minimal reproduction:
import SwiftUI
struct MyMenu: View {
var body: some View {
Menu("Test Menu") {
Button {
print("action 1")
} label: {
Text("Sub 1")
}
Button {
print("action 2")
} label: {
Text("Sub 2")
}
}
}
}
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
MyMenu()
}
.padding()
.toolbar {
MyMenu()
}
}
}
}
Share
Improve this question
asked Mar 13 at 15:18
Cameron LittleCameron Little
3,74927 silver badges38 bronze badges
1 Answer
Reset to default 0It works if you use placement .principal
for the toolbar item. However, the menu is then shown in the middle, instead of being on the right.
If you don't have any other toolbar items that are blocking the way, the menu can be pushed to the right side by applying .frame(maxWidth: .infinity, alignment: .trailing)
:
.toolbar {
ToolbarItem(placement: .principal) {
MyMenu()
.frame(maxWidth: .infinity, alignment: .trailing)
}
}
本文标签: tvOS SwiftUI Menu in toolbarStack Overflow
版权声明:本文标题:tvOS SwiftUI Menu in toolbar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744693820a2620155.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论