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

能否为CNContactViewController设置自定义backgroundColor?

CNContactViewController背景色设置无效的解决办法

可以设置,但直接修改controller.view.backgroundColor不会生效——因为CNContactViewController的视图层级里,自带的UITableView会铺满整个父视图,完全挡住你设置的背景色。要解决这个问题,需要同时修改内部tableView的背景色:

let controller = CNContactViewController(for: contact)
// 设置控制器主视图背景色
controller.view.backgroundColor = .orange
// 定位到内部的tableView并修改其背景色
if let tableView = controller.view.subviews.first(where: { $0 is UITableView }) as? UITableView {
    tableView.backgroundColor = .orange
    // 若需要让tableView透明,可设置tableView.backgroundView = nil
}

另外你代码里的layer.borderColor没生效,是因为缺少borderWidth设置,补上即可:

controller.view.layer.borderWidth = 1.0
controller.view.layer.borderColor = UIColor.blue.cgColor

内容的提问来源于stack exchange,提问作者In Sh.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:52:03