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

iOS 26中MKMarkerAnnotationView自定义标注被默认标注层遮挡的问题求助

iOS 26中MKMarkerAnnotationView自定义标注被默认标注层遮挡的问题求助

看起来你在iOS 26上碰到了MKMarkerAnnotationView的新默认子视图(MKMarkerBalloonView)遮挡自定义标注的糟心事,这确实是iOS新版本偷偷加的变化,我给你几个实用的解决思路:

1. 显式隐藏MKMarkerBalloonView

既然Xcode Inspector里已经看到这个新的默认子视图,我们可以在setupUI方法里遍历子视图,找到它并直接隐藏:

private func setupUI() {
    self.backgroundColor = .clear
    // 新增:定位并隐藏MKMarkerBalloonView
    self.subviews.forEach { subview in
        guard let balloonViewClass = NSClassFromString("MKMarkerBalloonView") else { return }
        if subview.isKind(of: balloonViewClass) {
            subview.isHidden = true
        }
    }
    self.addSubview(self.annotationView)
    self.annotationView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
    self.annotationView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -24).isActive = true
    self.bounds = .init(origin: .zero, size: .init(width: 0, height: 0))
}

这里用NSClassFromString是因为MKMarkerBalloonView属于私有API范畴,直接写类名会编译报错,这种写法既能避开编译依赖,又能在iOS 26上精准生效。

2. 直接改用MKAnnotationView(更推荐)

看你的代码,你其实完全没用到MKMarkerAnnotationView的默认marker功能——已经把tint、glyph、标题副标题全设为透明/隐藏了,不如直接换成基础的MKAnnotationView,从根源上避免iOS新版本加的默认子视图:

class AnnotationMarkerView: MKAnnotationView { // 把父类换成MKAnnotationView
    static let reuseId = "AnnotationMarkerView"
    
    private lazy var annotationView: UIView = {
        let hostController = UIHostingController(rootView: AnnotationView())
        let view: UIView = hostController.view
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .clear
        view.accessibilityElementsHidden = true
        return view
    }()
    
    init(annotation: MKAnnotation, reuseIdentifier: String?) {
        super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
        self.setupUI()
        self.displayPriority = .required
        self.collisionMode = .circle // MKAnnotationView同样支持碰撞检测
        self.backgroundColor = .clear
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        self.addSubview(self.annotationView)
        self.annotationView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.annotationView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -24).isActive = true
        // 可以根据自定义视图的实际大小设置bounds,让布局更稳定
        self.bounds = CGRect(origin: .zero, size: CGSize(width: 48, height: 48))
    }
}

这种方式最干净,彻底脱离了MKMarkerAnnotationView的默认特性,后续iOS再更新也不会再冒出来奇怪的子视图。

3. 快速应急:把自定义视图移到最顶层

如果想先快速验证效果,可以直接把自定义的annotationView移到视图层级的最前面,确保它在MKMarkerBalloonView之上:

private func setupUI() {
    self.backgroundColor = .clear
    self.addSubview(self.annotationView)
    // 新增:把自定义视图置顶
    self.bringSubviewToFront(self.annotationView)
    self.annotationView.centerXAnchor.constraint(equalTo: self.centerXAnchor, constant: 0).isActive = true
    self.annotationView.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: -24).isActive = true
    self.bounds = .init(origin: .zero, size: .init(width: 0, height: 0))
}

这个方法最简单,但依赖视图添加的顺序,可靠性不如前两种,适合快速测试。

另外提一句,你试的UIDesignRequiresCompatibility是针对Liquid Glass全局设计风格的兼容开关,和marker的子视图变化完全不相关,所以没效果是正常的~

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 07:34:36