我使用 UIView 创建了自定义上下文菜单。我试图在上下文菜单处于活动状态时更改角半径,但我不知道如何操作。我尝试过面膜,任何层,但没有任何效果。提供的当前代码不是圆角的。当我尝试将背景颜色设置为清晰时,在模糊颜色和内容之间出现了一种颜色,即背景颜色。有什么想法吗?
import SwiftUI
struct CustomContextMenu<Content: View>: View {
var content: Content
var menu: UIMenu?
var previewBackgroundColor: UIColor
init(
@ViewBuilder content: @escaping ()->Content,
actions: @escaping ()-> UIMenu?,
previewBackgroundColor: UIColor
) {
self.content = content()
self.menu = actions()
self.previewBackgroundColor = previewBackgroundColor
}
var body: some View {
content
.hidden()
.overlay(
ContextMenuHelper(content: content, actions: menu, previewBackgroundColor: previewBackgroundColor)
)
}
}
struct ContextMenuHelper<Content: View>: UIViewRepresentable
{
var content: Content
var actions: UIMenu?
var previewBackgroundColor: UIColor
init(content: Content, actions: UIMenu?, previewBackgroundColor: UIColor) {
self.content = content
self.actions = actions
self.previewBackgroundColor = previewBackgroundColor
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self )
}
func makeUIView(context: Context) -> UIView {
// uiview
let view = UIView()
view.backgroundColor = UIColor.clear
view.layer.cornerRadius = 20
// host view
let hostView = UIHostingController(rootView: content)
hostView.view.translatesAutoresizingMaskIntoConstraints = false
hostView.view.backgroundColor = .clear
view.addSubview(hostView.view) // add subview
view.addConstraints( // add constraints
[
hostView.view.topAnchor.constraint(equalTo: view.topAnchor),
hostView.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostView.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostView.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
hostView.view.widthAnchor.constraint(equalTo: view.widthAnchor),
hostView.view.heightAnchor.constraint(equalTo: view.heightAnchor)
]
)
if self.actions != nil { // if any menu item has been loaded
// interaction
let interaction = UIContextMenuInteraction(delegate: context.coordinator)
view.addInteraction(interaction)
}
return view
}
func updateUIView(_ uiView: UIView, context: Context) {
}
class Coordinator: NSObject, UIContextMenuInteractionDelegate {
var parent: ContextMenuHelper
init(parent: ContextMenuHelper) {
self.parent = parent
}
func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
interaction.view?.layer.cornerRadius = 30
guard let viewFrame = interaction.view?.frame else { return nil } // obtain child view size
return UIContextMenuConfiguration(identifier: nil) {
let previewController = UIHostingController(rootView: self.parent.content)
previewController.view.backgroundColor = self.parent.previewBackgroundColor
previewController.preferredContentSize = CGSize(width: viewFrame.width, height: viewFrame.height)
return previewController
} actionProvider: { items in
return self.parent.actions
}
}
}
}
由于您已经使用
UIHostingController
来创建预览内容,请确保您提供的 SwiftUI 视图具有所需的角半径和背景颜色。
let previewController = UIHostingController(rootView: self.parent.content
.background(self.parent.previewBackgroundColor)
.cornerRadius(30)
)
如果第一种方法由于视图的特定组成而不起作用,请考虑将内容包装在另一个应用背景和圆角半径的 SwiftUI 视图中,然后再传递给
UIHostingController
。这可以为样式提供更可控的上下文:
let previewView = self.parent.content
.background(Color(self.parent.previewBackgroundColor).edgesIgnoringSafeArea(.all))
.cornerRadius(30)
let previewController = UIHostingController(rootView: previewView)