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

如何移除iOS 16/Xcode 14中侧边栏自定义UICollectionViewListCell的蓝色边框

解决iOS 16/Xcode 14下UICollectionViewListCell侧边栏蓝色边框问题

我之前适配iOS 16侧边栏的时候也踩过这个坑!UICollectionViewListCell在侧边栏场景下会自动添加上蓝色的选中/聚焦边框,这是系统默认的侧边栏单元格交互样式导致的,下面给你几种简单有效的解决办法:

方法1:覆盖选中状态的背景配置

直接修改单元格的selectionConfiguration,把选中时的边框属性清空:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YourCellID", for: indexPath) as! YourCustomListCell
    
    // 配置基础内容
    var contentConfig = UIListContentConfiguration.sidebarCell()
    contentConfig.text = "侧边栏选项"
    cell.contentConfiguration = contentConfig
    
    // 移除选中时的蓝色边框
    var selectionConfig = UISelectionConfiguration.default
    let bgConfig = UIBackgroundConfiguration.listSidebarCell()
    bgConfig.borderWidth = 0
    bgConfig.borderColor = .clear
    selectionConfig.backgroundConfiguration = bgConfig
    cell.selectionConfiguration = selectionConfig
    
    return cell
}

方法2:替换单元格的背景配置(全局生效)

如果你的自定义cell所有场景都不需要边框,可以在cell的初始化方法里直接设置默认的背景配置:

class YourCustomListCell: UICollectionViewListCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupCustomBackground()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupCustomBackground()
    }
    
    private func setupCustomBackground() {
        // 正常状态背景
        var bgConfig = UIBackgroundConfiguration.listSidebarCell()
        bgConfig.borderWidth = 0
        bgConfig.borderColor = .clear
        backgroundConfiguration = bgConfig
        
        // 选中状态背景
        var selectedBgConfig = UIBackgroundConfiguration.listSidebarCell()
        selectedBgConfig.borderWidth = 0
        selectedBgConfig.borderColor = .clear
        selectedBackgroundConfiguration = selectedBgConfig
    }
}

方法3:移除聚焦状态的边框(针对iPad/mac Catalyst场景)

如果边框是在单元格获取焦点时出现的,可以直接禁用聚焦视觉样式:

// 在cellForItemAt方法里添加这一行
cell.focusVisualStyle = nil

这几种方法都能有效移除那个烦人的蓝色边框,你可以根据自己的业务场景选择最合适的方案~

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 15:27:45