You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI自定义UIView上下文菜单圆角设置问题求助

自定义UIView上下文菜单圆角半径修改问题

我用UIView创建了自定义上下文菜单,尝试修改菜单激活状态下的corner radius(圆角半径),试了mask、layer等各种方法都没用,当前代码无法实现圆角效果。把背景色设为透明时,模糊背景和内容之间还会出现多余的背景色。

不理想的效果

上下文菜单圆角异常效果
(注:可见预览视图的圆角不生效,边缘透出直角的模糊背景)

当前代码

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
            }
        }
    }
}

编辑补充:包裹视图后的问题

把视图嵌套到另一个视图中时,只有内部内容视图的圆角生效了,外部的模糊背景容器还是直角:
仅内部视图有圆角的效果

修改后的代码片段

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 previewView = self.parent.content
            .background(Color(self.parent.previewBackgroundColor).edgesIgnoringSafeArea(.all))
            .cornerRadius(30)
        
        let previewController = UIHostingController(rootView: previewView)
        
        previewController.preferredContentSize = CGSize(width: viewFrame.width, height: viewFrame.height)
        
        return previewController
    } actionProvider: { items in
        return self.parent.actions
    }
}

内容的提问来源于stack exchange,提问作者Zajebany Sifl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 01:55:23