在SwiftUI中,有一个叫做菜单的东西,你可以在里面有按钮、分隔符、其他菜单等等.下面是我正在构建的一个例子:

import SwiftUI

func testing() {
    print("Hello")
}

struct ContentView: View {
    var body: some View {
        VStack {
            Menu {
                Button(action: testing) {
                    Label("Button 1", systemImage: "pencil.tip.crop.circle.badge.plus")
                }
                Button(action: testing) {
                    Label("Button 2", systemImage: "doc")
                }
            }
        label: {
            Label("", systemImage: "ellipsis.circle")
        }
        }
    }
}

因此,在SwiftUIplayground 应用程序中,他们有这个菜单:

enter image description here

我的问题是:

How did they make the circled menu option?我还在菜单中发现了几个类似于这组水平按钮的例子,如下所示:

enter image description here

HStack和其他明显的try 都以失败告终.我已经考虑过添加MenuStyle,但苹果的文档非常缺乏,只显示了一个在菜单按钮上添加红色边框的示例.反正我也不确定这条路是不是对的.

我只能让分隔符()和按钮()显示在菜单中:

enter image description here

我也只能找到显示这两个选项的代码示例,尽管在应用程序中看到了其他选项的示例.

推荐答案

这看起来目前只有UIKit(并且只有iOS 16+)可用,通过设置

menu.preferredElementSize = .medium

要将其添加到您的应用程序,您可以将UIMenu添加到UIButton,然后使用UIHostingController将其添加到您的SwiftUI应用程序.

下面是一个实现示例:

Subclass a UIButton

class MenuButton: UIButton {
    
    override init(frame: CGRect) {
        super.init(frame: frame)

        let inspectAction = self.inspectAction()
        let duplicateAction = self.duplicateAction()
        let deleteAction = self.deleteAction()

        setImage(UIImage(systemName: "ellipsis.circle"), for: .normal)
        menu = UIMenu(title: "", children: [inspectAction, duplicateAction, deleteAction])
        menu?.preferredElementSize = .medium
        showsMenuAsPrimaryAction = true

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func inspectAction() -> UIAction {
        UIAction(title: "Inspect",
                 image: UIImage(systemName: "arrow.up.square")) { action in
           //
        }
    }
        
    func duplicateAction() -> UIAction {
        UIAction(title: "Duplicate",
                 image: UIImage(systemName: "plus.square.on.square")) { action in
           //
        }
    }
        
    func deleteAction() -> UIAction {
        UIAction(title: "Delete",
                 image: UIImage(systemName: "trash"),
            attributes: .destructive) { action in
           //
        }
    }

}

Create a 100 using UIViewRepresentable

struct Menu: UIViewRepresentable {
    func makeUIView(context: Context) -> MenuButton {
        MenuButton(frame: .zero)
    }
    
    func updateUIView(_ uiView: MenuButton, context: Context) {
    }
}

Works like a charm!

struct ContentView: View {
    var body: some View {
        Menu()
    }
}

enter image description here

Ios相关问答推荐

Firebase SDK 10+—无法安装软件包

如何在iPhone 15上消除.NET Maui中状态栏和页面之间的差距

在iOS 17脉冲符号效果不起作用的情况下制作UIBarButtonItem动画(没有UIImageView)

由于代码签名时出错,无法在iOS设备上发布iOS Maui Build

通过拖动更改视图位置的SwiftUI会引发UI错误

我应该使用哪个UIButton发送事件在SWIFT上刷卡?

如何在本地iOS没有插件的情况下处理平台线程?

如何创建装饰视图?

在Xcode 15中为具有@Binding的视图创建SwiftUI #预览的方法

我无法使用 Swift 将文件上传到 firebase

视图之间的 SwiftUI 过渡,没有过渡

如何在 Swift 中旋转 UIButton 和 UILabel 的文本?

如何在 SwiftUI 中检测 TextField 的实时变化?

如何在 UIActivityViewController 中设置邮件主题?

使用导航控制器在prepareForSegue中设置数据

SwiftUI - 列表行中的多个按钮

如何使用 AFNetworking 设置超时

Swift:如何在设备旋转后刷新 UICollectionView 布局

NSURL 到带有 XCTest 的测试包中的文件路径

如何在 Swift 中更改按钮标题对齐方式?